Reputation: 386
I have the following xml-file:
<Book description="for beginners" name="IT Book">
<Available>yes</Available>
<Info pages="500.</Info>
</Book>
I want it to look like this:
<Book description="for pros" name="IT Book">
<Available>yes</Available>
<Info pages="500.</Info>
</Book>
I looked up how to modify xml-documents properly on the internet. I found out that first of all I should declare a template for just copying everything:
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
However, I dont know how to write the template for the actual modification. Thanks for helping out a beginner.
EDIT: Here is my Stylesheet so far (as requested by 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>
Upvotes: 2
Views: 8077
Reputation: 2167
This question is already answered in many other threads. Eg. XSLT: How to change an attribute value during <xsl:copy>?
In your case, you need a template, which matches on your attribute description
, besides the identity-copy template.
<xsl:template match="@description"> <!-- @ matches on attributes, possible to restrict! -->
<xsl:attribute name="{name()}"> <!-- creates a new attribute with the same name -->
<xsl:text>for pros</xsl:text> <!-- variable statement to get your desired value -->
</xsl:attribute>
</xsl:template>
EDIT 1 (further information cause of errors)
One complete, valid, runnable script would 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>
Upvotes: 6