I am trying to add an attribute to a node selected from the source XML. My initial attempt is below, but I think I am misund开发者_如何学编程erstanding the concept. Do I need to load the node into a variable first, or is there another way to do this?
Source XML
<root>
<BigImage>
<img alt="Flower" src="/flower.jpg" />
</BigImage>
</root>
Desired output
<img alt="Flower" src="/flower.jpg" class="image-left" />
Current (incorrect) XSLT
<xsl:template match="root">
<xsl:copy-of select="./BigImage/node()" />
<xsl:attribute name="class">image-left</xsl:attribute>
</xsl:template>
Current (incorrect) output
<img alt="Flower" src="/flower.jpg" />
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="img">
<img class="image-left">
<xsl:copy-of select="@*"/>
</img>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<root>
<BigImage>
<img alt="Flower" src="/flower.jpg" />
</BigImage>
</root>
produces exactly the wanted, correct result:
<img class="image-left" alt="Flower" src="/flower.jpg"/>
精彩评论