schwarz
schwarz

Reputation: 521

Changing the value of an attribute of the document element in an xml file using XSLT

I am trying to change the value of an attribute of the document element in an XML file using XSLT transformation. For example,

<?xml version="1.0" encoding="UTF-8"?>
<ns1:xmlgMsc xmlns:ns1="org.example" formatVersion="1.0" name="BlaBlah" pathName="/system/abc.xml" writtenBy="Me me me">
   <ns1:blockRoot someAtt="0" anotherAtt="1" />
</ns1:xmlgMsc>

Here I would like to change the "pathName" to some another path (say "/local/xyz.xml"). Can somebody please provide the syntax or point me into the right direction for doing this in XSLT?

Thanks in advance!

Upvotes: 1

Views: 175

Answers (1)

Tomalak
Tomalak

Reputation: 338376

You will need a stylesheet that consists of two templates. The identity template (look it up) and this one:

<xsl:template match="/*/@pathName">
  <xsl:attribute name="{name()}">
    <xsl:value-of select="'/local/xyz.xml'" />
  </xsl:attribute>
</xsl:template>

You could use an <xsl:param> to pass in the new path dynamically, if you don't want to hard-code the new value.

Minor correction: The root node (/) of an XML document does not have attributes. You mean the document element (/ns1:xmlgMsc), that's one level down the hierarchy.

Upvotes: 3

Related Questions