Given the input file shown below, the first code example works, and the second code example fails with error:
Unexpected token '{' in the expression. -->{<-- name(.)}
The main difference is whether I put the {name(.)}
in a name attribute or a select attribute. I need to generate the data like the second code to match my OAGIS ProcessShipment schema.
<xsl:for-each select="//s0:B2">
<xsl:for-each select="./*">
<xsl:if test=".">
<xsl:element name="{name(.)}" >
<xsl:value-of select="." />
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
The code that fails:
<xsl:for-each select="//s0:B2">
<xsl:for-each select="./*">
<xsl:if test=".">
<xsl:element name="ID" xmlns="http://www.openapplications.org/oagis/10">
<xsl:attribute name="typeCode">
<xsl:value-of select="{name(.)}" />
</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
Desired Output:
<ID typeCode="B202>ABCD</ID>
<ID typeCode="B204>0080082626</ID>
<ID typeCode="B206>PP</ID>
Input data:
<ns0:X12_00401_204 xmlns:ns0="http://example.com/X12/204">
<ST>
<ST01>204</ST01>
<ST02>0001</ST02>
</ST>
<ns0:B2>
<B202>ABCD</B202>
<B204>0080082626</B204>
<B206>PP</B206>
</ns0:B2>
</ns0:X12>
Reference: How to select each child node of a parent in a for-each xslt statement?
By trial and error I got this to work:
<xsl:value-of select="name(.)" />
Just removed the curly braces, which tell it to run what is inside as braces as xpath. I'm still a little fuzzy why it is needed in the name= though.