我是下面的XML示例行。
情况1:
<para><content-style font-style="bold">1.54</content-style> For the purposes of this book, the only authorities that are strictly speaking decisive are cases decided by the Singapore courts and their predecessors, and the earlier binding decisions of the Privy Council. This relative freedom from authority has its good and bad points. On the minus side, there is often a penumbra of uncertainty surrounding a proposition based upon a foreign case; until our courts have actually accepted the proposition, it can only be treated as tentative. On the plus side, we are not bound to follow a case that is wrong in principle or weak in reasoning. Our courts are at liberty to develop and interpret the law in a manner that is suitable to Singapore’s needs.<page num="17"/></para>
案例2:
<para><page num="5"/><content-style font-style="bold">1.12</content-style> In the context of the PA, the term ‘firm’ refers collectively to those who entered into partnership with one another and the name under which partners carry on their business (i.e. name of their partnership) is referred to as the
情形3:
<para><page num="5"/><content-style font-style="bold">1.12</content-style> In the context of the PA, the term ‘firm’ refers collectively to those who entered into partnership with one another and the name under which partners carry on their business (i.e. name of their partnership) is referred to as the <page num="6"/>
并且我使用以下XSLT行来应用模板。
<xsl:apply-templates select="child::node()[not(self::content-style[1] and self::content-style[1]/preceding::page)]"/>
此处我想要实现的是,将模板应用于para
内容,使page
的第一个子节点para
先于content-style
除此之外,虽然还有其他page
模板应该可以正常工作。但在我的情况下,page
,para
之前content-style
的第一个孩子也被抓住了。
请让我知道我哪里出错了。
在这种情况下,案例1的输出应该捕获page
,而在第二种情况下,不应该捕获page
而在案例3中,page num="5"
应该被忽略并且应该抓住page num="6"
由于
答案 0 :(得分:1)
最初忽略页面的条件,您检查第一个内容样式的当前条件无效...
<xsl:apply-templates select="child::node()[not(self::content-style[1])]" />
所有内容风格元素都适用。在这种情况下,[1]
条件不是节点在其父节点内的位置,而是与刚刚选择的节点相关,并将分别针对每个内容样式进行评估。因此,上述代码根本无法实现您的期望。
要测试节点是否相等,请考虑先设置变量以保存第一个内容样式的唯一ID
<xsl:variable name="content" select="generate-id(content-style[1])" />
然后,您的 xsl:apply-templates 最初将如下所示
<xsl:apply-templates select="child::node()[not(generate-id() = $content)]" />
要扩展此功能以应对页面元素,请检查以下第一个内容样式也没有相同的ID ..
<xsl:apply-templates select="child::node()
[not(generate-id() = $content or self::page[generate-id(following-sibling::content-style[1]) = $content])]"/>
另一种方法也是可能的。不是主动选择所需的节点,而只选择所有节点,但要使用模板匹配来排除您不想要的节点。用这个替换你 xsl:apply-tempates ......
<xsl:apply-templates />
然后在代码中添加以下两个模板:
<xsl:template match="content-style[not(preceding-sibling::content-style)]" />
<xsl:template match="page[following-sibling::content-style and not(preceding-sibling::content-style)]" />