我有下面这行XML样例。
Case1:
<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
Case3:
<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内容,将para的第一个子节点的第一个page保留在content-style之前,除此之外,尽管有任何其他page,但模板应该可以很好地工作。但在这里,在我的例子中,para的第一个子对象page在content-style之前也被捕获了。
请让我知道我哪里错了。
在这种情况下,第一种情况的输出应该捕获page,第二种情况的输出不应该捕获page,第三种情况的输出应该忽略page num="5",而应该捕获page num="6"
谢谢
发布于 2014-06-03 21:11:15
最初忽略页面的条件,您当前检查第一个内容样式的条件将不起作用……
<xsl:apply-templates select="child::node()[not(self::content-style[1])]" />这将适用于所有内容样式的元素。本例中的[1]条件不是节点在其父节点中的位置,而是与刚刚选择的节点相关,并且将分别为每个内容样式的求值。因此,上面的代码根本做不到您所期望的事情。
要测试节点的相等性,可以考虑首先设置一个变量来保存第一个content-style的唯一id
<xsl:variable name="content" select="generate-id(content-style[1])" />然后,您的xsl:apply-templates最初将如下所示
<xsl:apply-templates select="child::node()[not(generate-id() = $content)]" />要扩展此元素以处理页面元素,请检查下面的第一个content-style是否也不具有相同的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)]" />https://stackoverflow.com/questions/24015026
复制相似问题