我正在尝试选择一个元素,其文本包含3个以上具有特定值的注释。
我试过了:
//*[count(self::comment()[. = "comment_text"]) > 3]
和
//*[count(comment()[. = "comment_text"]) > 3]
并没有成功。我究竟做错了什么?
任何帮助,不胜感激。
您的方法适用于XSLT 2.0-您没有说明要使用哪个版本。
但是,通常的做法是将>
字符转义到该字符,gt
并且必须注意嵌套的引号(如果嵌套在双引号内,则引号必须为单引号,反之亦然)。
样本输入XML
<?xml version="1.0" encoding="utf-8"?>
<root>
<true><!--comment_text--><!--comment_text--><!--comment_text--><!--comment_text--></true>
<wrong1><!--comment_text--><!--comment_text--><!--comment_text--></wrong1>
<wrong2><!--comment_text--><!--comment_text--><!--comment_text--><!--wrong_text--></wrong2>
</root>
样式表
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*[count(comment()[.='comment_text']) gt 3]">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>
输出
<?xml version="1.0" encoding="UTF-8"?>
<true/>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句