test.xml:
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<root>
<x id="x1">a'b"c</x>
<x id="x2">a'bc</x>
<x id="x3">ab"c</x>
</root>
test.xsl:
此时如果要查询某个x节点时,有个遇到的问题语句: <xsl:value-of select="//x[text()='a'bc']/@id"/>,这时XPATH的引号与数据中的引号发生了冲突。
解决办法1:
假如 ' 和 " 不同时出现情况下,我们可以这么写:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="x">a'b"c</xsl:variable>
x2:<xsl:value-of select="//x[text()="a'bc"]/@id"/>
<br/>
x2:<xsl:value-of select='//x[text()="a'bc"]/@id'/>
<hr/>
x3:<xsl:value-of select="//x[text()='ab"c']/@id"/>
<br/>
x3:<xsl:value-of select='//x[text()='ab"c']/@id'/>
</xsl:template>
</xsl:stylesheet>
这种方法中,遇到了 ' 和 "同时出现将会显得无能为力。
解决办法2:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="x">a'b"c</xsl:variable>
x1:<xsl:value-of select="//x[text()=$x]/@id"/>
</xsl:template>
</xsl:stylesheet>
这种方法适合所有情况,不过必须要通过变量。