I have an XML document which I’m subjected to an XSLT. The structure is similar to:
<root>
<item value="1">
<object/>
</item>
<item value="2" />
<object/>
</item>
</root>
My goal is to end up with a transformed XML similar to:
<root>
<parent>
<object-one value-one="1"/>
</parent>
<parent>
<object-two value-two="2"/>
</parent>
</root>
My XSLT is similar to:
<xsl:apply-templates select="object" />
<xsl:template match="object">
<xsl:call-template name="1" />
<xsl:call-template name="2" />
</xsl:template>
<xsl:template name="1" match="object[item/@value = '1'">
<xsl:element name="object-one" namespace="http://something.org">
<xsl:attribute name="_Description">
<xsl:value-of select="@_Type"/>
</xsl:attribute>
<xsl:attribute name="_Type">
<xsl:value-of select="@_Amount"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template name="2" match="object[item/@value = '2'">
<xsl:element name="object-two" namespace="http://something.org">
<xsl:attribute name="OriginalAmount">
<xsl:value-of select="@_Amount"/>
</xsl:attribute>
</xsl:element>
</xsl:template>
The problem is the all item nodes are having the same template applied. How can I apply a template to only specific nodes?
EDIT: Now for different input sample (corrected for well-formed):
This stylesheet:
Output:
EDIT 2: Now, what is wrong within your stylesheet fragment? Well, it looks like you don’t know how the processor resolves template rules applying, also XPath navegation.
First, this
object[item/@value = '1']will match only this kind of inputSecond, consider this three rules
1 –
2 –
3 –
With your last provided input, first
objectelement (in document order) will match rules 1 and 2, and then the processor would resolve to apply rule 2. Why? From http://www.w3.org/TR/xslt#conflict