xml – Transform & in XSLT? – Education Career Blog

I’m relatively new to XSLT. I’ve come across an issue that I don’t know how to get around. I have an pretty large XML document that I am trying to transform to into another smaller, refined XML document.

The large XML document has this style:

 <Property>
  <name>Document name</name>
  <value>SomeValue</value>   
 </Property>
...
 <Property>
  <name>Document Title</name>
  <value>Me %amp; you</value>   
 </Property>

How do you transform the value in between the value elements and keep the “&amp”; intact. Apparently transforming this XML is causing errors due to that ampersand escape in the text.

Note: This large XML is generated by a application that pulls data from a server. So I’m kinda of stuck dealing with the escape ampersand 🙁

,

Here is a simple (and very fundamental) example, using the identity rule:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML file:

<t>
 <Property>
  <name>Document name</name>
  <value>SomeValue</value>
 </Property>
...
 <Property>
  <name>Document Title</name>
  <value>Me &amp; you</value>
 </Property>
</t>

it is transformed into itself (identity):

<t>
   <Property>
      <name>Document name</name>
      <value>SomeValue</value>
   </Property>
...
 <Property>
      <name>Document Title</name>
      <value>Me &amp; you</value>
   </Property>
</t>

and the character &amp; is preserved.

Leave a Comment