Change value of an XML attribute via xslt -
i have following xml-file:
<book description="for beginners" name="it book"> <available>yes</available> <info pages="500.</info> </book>
i want this:
<book description="for pros" name="it book"> <available>yes</available> <info pages="500.</info> </book>
i looked how modify xml-documents on internet. found out first of should declare template copying everything:
<xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template>
however, dont know how write template actual modification. helping out beginner.
edit: here stylesheet far (as requested ul1):
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:sig="http://www.w3.org/2000/09/xmldsig#"> <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/> <xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*"/> </xsl:copy> </xsl:template> <xsl:template match="@description='for beginners'"> <xsl:attribute name="{name()}"> <xsl:text>for pros</xsl:text> </xsl:attribute> </xsl:template> </xsl:stylesheet>
this question answered in many other threads. eg. xslt: how change attribute value during <xsl:copy>?
in case, need template, matches on attribute description
, besides identity-copy template.
<xsl:template match="@description"> <!-- @ matches on attributes, possible restrict! --> <xsl:attribute name="{name()}"> <!-- creates new attribute same name --> <xsl:text>for pros</xsl:text> <!-- variable statement desired value --> </xsl:attribute> </xsl:template>
edit 1 (further information cause of errors)
one complete, valid, runnable script be:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="@description[. = 'for beginners']"> <xsl:attribute name="{name()}"> <xsl:text>for pros</xsl:text> </xsl:attribute> </xsl:template> </xsl:stylesheet>
Comments
Post a Comment