1

I'm attempting to add a child node referencing another child node. In the below example I'd like to populate <AcctID> from <AcctDtl> into <Position>. I'm doing this as MS-Access only imports child nodes into separate tables without a way of referencing/linking the tables.

<AcctFncl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="fsrv" xsi:schemaLocation="fsrv Rec.xsd" Version="27">
  <CreateDate>20151101</CreateDate>
  <EffectDate>20151031</EffectDate>
  <FnclRec>
    <AcctDtl>
      <MgmtCode>XXX</MgmtCode>
      <AcctID>123980</AcctID>      
    </AcctDtl>
    <Position>
      <FundID>5268</FundID>
      <TotalUnAssigned>50</TotalUnAssigned>
      <TotalAssigned>0</TotalAssigned>
      <AveCost>10</AveCost>
      <DivOpt>1</DivOpt>
    </Position>
   </FnclRec>
</AcctFncl>

should look like:

<AcctFncl xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="fsrv" xsi:schemaLocation="fsrv Rec.xsd" Version="27">
  <CreateDate>20151101</CreateDate>
  <EffectDate>20151031</EffectDate>
  <FnclRec>
    <AcctDtl>
      <MgmtCode>XXX</MgmtCode>
      <AcctID>123980</FundAcctID>      
    </AcctDtl>
    <Position>
      <AcctID>123980</AcctID>      
      <FundID>5268</FundID>
      <TotalUnAssigned>50</TotalUnAssigned>
      <TotalAssigned>0</TotalAssigned>
      <AveCost>10</AveCost>
      <DivOpt>1</DivOpt>
    </Position>
   </FnclRec>
</AcctFncl>

I've been trying to create a XSLT to do this but I think I'm in over my head here.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Position">
        <FundPosition>
            <AcctID><xsl:value-of select="../AcctDtl/CreateDate"/></AcctID>         
            <xsl:apply-templates select="@*|node()"/>
        </FundPosition>
    </xsl:template>
</xsl:stylesheet>
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
NO CARRIER
  • 13
  • 2
  • Sorry, that should read 123980. My mistake. The XML is coming from a third party system, and I'm attempting to import it into MS Access and have a key to link both child tables on import. Access imports the separate tables, but no common key to link them. – NO CARRIER Dec 01 '17 at 20:26

1 Answers1

0

I think the major problem you're having is that your XML input is in the default namespace fsrv, but your XSLT doesn't handle it.

In order to match those elements in your XSLT you'll have to bind that namespace to a prefix and use that prefix in your XPaths.

In the example below, I used the prefix "f", but you could use something else.

Example...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:f="fsrv">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="f:Position">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()|../f:AcctDtl/f:AcctID"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Demo: http://xsltransform.net/bEJaofi

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • 1
    That worked! I'm not sure exactly how the namespace works (or how it's used in XML files), reading up on it now so I can understand this better. Also thanks for showing me the xlstransform.net site, also very useful compared to the Notepad++ I'm using now... – NO CARRIER Dec 01 '17 at 20:51
  • @NOCARRIER - You're very welcome! +1 for a great first question. Here are a couple of good resources on namespaces: http://www.jclark.com/xml/xmlns.htm https://stackoverflow.com/a/25830482/317052 – Daniel Haley Dec 01 '17 at 20:59