我有两种不同的情况
案例A
<cell>
text here and there
<break/>
text here and there
<break/>
</cell>
和案例B
<cell>
text here and there
<break/>
text here and there
<break/>
text here and there
</cell>
我需要一个Xpath条件,它将选择CASE A中的最后一个中断而不是CASE B中的中断,以便我可以使用XSLT删除它
某种东西
//cell/break[position() = last()]
几乎可以完成这项工作,但我需要排除那些背后有文字的人。
有什么建议吗?
答案 0 :(得分:2)
使用如下表达式:
break[position() = last() and not(following-sibling::text()[normalize-space(.) != ''])]
用简单的英语表示:
选择上下文中的最后一个
break
元素 - 但前提是不是后跟一个只包含空格的文本节点。
正如@keshlam所建议的那样,最后一个条件是必要的,因为换行符也是文本节点。但是,当""
应用于normalize-space()
时,仅包含空格的文本节点将等于<cell>
text here and there
<break/>
text here and there
<break type="wrong"/>
text here and there
</cell>
。
假设以下输入:
输入1
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="break[position() = last() and not(following-sibling::text()[normalize-space(.) != ''])]">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
<强>样式表强>
<cell>
text here and there
<break/>
text here and there
<break type="correct"/>
</cell>
将(正确)不会产生任何输出。另一方面,
输入2
<?xml version="1.0" encoding="utf-8"?>
<break type="correct"/>
的产率:
输出2
{{1}}
答案 1 :(得分:2)
尝试:
break[not(following-sibling::node())]