XSLT Parsing XML with &lt and &gt

一个人想着一个人 提交于 2020-08-20 05:59:27

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/41434403/xslt-parsing-xml-with-lt-and-gt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!