1

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am afraid it didn't work me ... any idea if I am doing anything wrong? This is a small xslt I made; <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template> <xsl:template match="/*/@pathName"> <xsl:copy> <xsl:value-of select="'/local/xyz.xml'" /> </xsl:copy> </xsl:template> </xsl:stylesheet>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.