问题
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
<TextBlock>
<config>This is a config.</config>
<path>This is a file path.</path>
</TextBlock>
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 < >.
Is there a way to get around this or convert <
and >
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, '<path>'), '</path>')"/>
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/</</g" input.xml | sed -e "s/>/>/g" > output.xml
and then process output.xml
with the XSLT processor of your choice.
This would convert all the <
and >
entities to the respective chars.
来源:https://stackoverflow.com/questions/41434403/xslt-parsing-xml-with-lt-and-gt