user2628187
user2628187

Reputation: 307

XSLT Parsing XML with &lt and &gt

I have an XML document that has a TextBlock that contains the below samplecode.

<TextBlock>
  <config>This is a config.</config>
  <path>This is a file path.</path>
</TextBlock>

The actual XML file contains the below

&lt;TextBlock&gt;
  &lt;config&gt;This is a config.&lt;/config&gt;
  &lt;path&gt;This is a file path.&lt;/path&gt;
&lt;/TextBlock&gt;

I'm trying to get the value of path tag using XSLT 1.0

<h1>
  <xsl:value-of select="/TextBlock/path" disable-output-escaping="yes"/>
</h1>

I don't get the value because the XML structure is broken due to &lt &gt.
Is there a way to get around this or convert &lt and &gt to < and > in XSLT?

Upvotes: 1

Views: 4750

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116959

Escaped XML is just a meaningless text string and cannot be parsed using XPath.

In the given example, you could use:

<xsl:value-of select="substring-before(substring-after(/TextBlock, '&lt;path&gt;'), '&lt;/path&gt;')"/>

to extract the string "This is a file path."from the given document.


Alternatively, you could process the document twice: in the first pass, use disable-output-escaping to unescape the string, then use a second stylesheet on the resulting file to extract the contents of what will now be the path element.


Added:

If your entire document is escaped as shown in your edited example, then it isn't an XML document and cannot be processed by XSLT (at least not XSLT 1.0) at all.

Upvotes: 1

zx485
zx485

Reputation: 29022

If you'd have sed available, you could pipe your files through it before processing it with XSLT.

For example: to process input.xml to output.xml you would execute

sed -e "s/&lt;/</g" input.xml | sed -e "s/&gt;/>/g" > output.xml

and then process output.xml with the XSLT processor of your choice.
This would convert all the &lt; and &gt; entities to the respective chars.

Upvotes: 0

Related Questions