I'm kinda stuck on a transformation i'd like to make on a xml file.
Basically i'm trying to copy all the xml but change some tags which only begins like this
XML code :
<test alt="foo" title="bar"/>
What i'd like to get after passing the xsl :
<test alt="foo"/>
Or
<change alt="foo" title=""/>
Thing is, sometimes i got tag with a lot of attributes, so i dont want to make a template match and then change every attributes manually.
Actually i'm doing this :
<xsl:template match="test">
<change><xsl:apply-templates select="@*|node()"/></change>
</xsl:template>
<xsl:template match="test/@title">
<xsl:attribute name="title">
<xsl:value-of select=""/>
</xsl:attribute>
</xsl:template>
But it doesnt change the content of title in the output.
For all such tasks you should start with the identity transformation template
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
and then add templates for nodes that need special treatment, for instance
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="test/@title"/>
would copy everything unchanged but would delete all title
attributes of test
elements.
Or
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="test">
<change><xsl:apply-templates select="@*|node()"/></change>
</xsl:template>
<xsl:template match="test/@title">
<xsl:attribute name="title"/>
</xsl:template>
should implement your second requirement. If you still have problems then post minimal but complete samples allowing us to reproduce the problem.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments