0
votes

I'm new to xslt, and I'm out of ideas now. Hopefully you peeps can help me back on track.

Given the following XML which is delivered to us from 3rd party:

<orderbatch>
<orders>
<order>
<id>1</id>
<customerid>2001</customerid>
<articleid>345</articleid>
</order>
</orders>
<customers>
<customer>
<id>2001</id>
<name>John Smith</name>
</customer>
</customers>
</orderbatch>

if I want to move the corresponding customer node under the order node via xslt, to get this output:

<orderbatch>
<orders>
<order>
<id>1</id>
<customerid>2001</customerid>
<articleid>345</articleid>
<customer>
<id>2001</id>
<name>John Smith</name>
</customer>
</order>
</orders>
<customers>
<customer>
<id>2001</id>
<name>John Smith</name>
</customer>
</customers>
</orderbatch>

I have a working xslt with a fixed value, but I'm a little at a loss how to parameterize it:

<xsl:output method="xml"/>

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

<xsl:key name="customerList" match="/orderbatch/customers/customer" use="id"/>

<xsl:template match="order">
    <xsl:copy>
        <xsl:value-of select="id"/>
        <customer>
            <xsl:value-of select="key('customerList', '2001')"/>            
        </customer>
    </xsl:copy>
</xsl:template>

For some reason, I cannot use xsl:variable, current()/customerid instead of the '2001', as those result in an empty node at the order node (while the customer is present in the customerlist). So, the question is, how do I parameterize the '2001' in the select of the copy block? (or am I completely doing something wrong here?)

1

1 Answers

0
votes

For copying your customer, don't use value-of but copy-of.

<xsl:template match="order">
    <xsl:copy>
        <xsl:copy-of select="customerid"/>
        <xsl:copy-of select="key('customerList', customerid)" />
    </xsl:copy>
</xsl:template>

BTW, current() is not mandatory here, as order is the context node.