Xml 如何在XSLT2.0中通过xpath评估注释数据?
是否可以通过xpath将注释数据作为节点进行求值。 下面是我在输入xml中的输入部分Xml 如何在XSLT2.0中通过xpath评估注释数据?,xml,xslt-2.0,Xml,Xslt 2.0,是否可以通过xpath将注释数据作为节点进行求值。 下面是我在输入xml中的输入部分 Input: <htmlBody> <!-- @charset "utf-8"; <toc> <sections> <section id="abc"> <title> some title </title> &
Input:
<htmlBody>
<!-- @charset "utf-8";
<toc>
<sections>
<section id="abc">
<title> some title </title>
<pageBreak/>
</section>
...
</toc>
-->
</htmlBody>
The required output should be:
<htmlBody>
<toc>
<sections>
<section id="ab">
<title>...</title>
<pageBreak/>
</section>
...
</toc>
</htmlBody>
输入:
所需输出应为:
...
...
然后我可以通过xpath提取所有标题,即htmlBody/toc//title。
我尝试过exslt:node set,但没有成功。如果XSLT 2.0处理器支持禁用输出转义,您可以执行以下操作:
<xsl:template match="htmlBody/comment()">
<xsl:value-of select="substring-after(., '@charset "utf-8";')" disable-output-escaping="yes"/>
</xsl:template>
您是否尝试过
comment()
功能,例如
?不过,这会将注释作为字符串提供给您。我认为它不会直接作为节点提供。但是,在XSLT2.0中,您可以在以后将其转换为节点树。是的,我已经尝试过了,但无法将其转换为节点树。感谢您建议使用扩展函数saxon:parse。现在它工作正常了。@ArvindKr,如果我的答案解决了您的问题,那么您应该接受它作为一个答案,以便在堆栈溢出时将该问题标记为已解决。
<xsl:template match="htmlBody/comment()">
<xsl:copy-of select="saxon:parse(substring-after(., '@charset "utf-8";'))"/>
</xsl:template>
<xsl:template match="htmlBody/comment()">
<xsl:copy-of select="saxon:parse(substring-after(., '@charset "utf-8";'))//title"/>
</xsl:template>