首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >忽略案例并应用模板

忽略案例并应用模板
EN

Stack Overflow用户
提问于 2014-06-03 20:19:08
回答 1查看 51关注 0票数 0

我有下面这行XML样例。

Case1:

代码语言:javascript
复制
  <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&#x2019;s needs.<page num="17"/></para>

案例2:

代码语言:javascript
复制
<para><page num="5"/><content-style font-style="bold">1.12</content-style> In the context of the PA, the term &#x2018;firm&#x2019; 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:

代码语言:javascript
复制
<para><page num="5"/><content-style font-style="bold">1.12</content-style> In the context of the PA, the term &#x2018;firm&#x2019; 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行来应用模板。

代码语言:javascript
复制
<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的第一个子对象pagecontent-style之前也被捕获了。

请让我知道我哪里错了。

在这种情况下,第一种情况的输出应该捕获page,第二种情况的输出不应该捕获page,第三种情况的输出应该忽略page num="5",而应该捕获page num="6"

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-06-03 21:11:15

最初忽略页面的条件,您当前检查第一个内容样式的条件将不起作用……

代码语言:javascript
复制
<xsl:apply-templates select="child::node()[not(self::content-style[1])]" />

这将适用于所有内容样式的元素。本例中的[1]条件不是节点在其父节点中的位置,而是与刚刚选择的节点相关,并且将分别为每个内容样式的求值。因此,上面的代码根本做不到您所期望的事情。

要测试节点的相等性,可以考虑首先设置一个变量来保存第一个content-style的唯一id

代码语言:javascript
复制
<xsl:variable name="content" select="generate-id(content-style[1])" />

然后,您的xsl:apply-templates最初将如下所示

代码语言:javascript
复制
<xsl:apply-templates select="child::node()[not(generate-id() = $content)]" />

要扩展此元素以处理页面元素,请检查下面的第一个content-style是否也不具有相同的id。

代码语言:javascript
复制
 <xsl:apply-templates select="child::node()
      [not(generate-id() = $content or self::page[generate-id(following-sibling::content-style[1]) = $content])]"/>

另一种方法也是可能的。不是主动选择您想要的节点,而是选择所有节点,但使用模板匹配来排除您不想要的节点。用这个替换你的xsl:apply-tempates ...

代码语言:javascript
复制
<xsl:apply-templates />

然后在代码中添加以下两个模板:

代码语言:javascript
复制
<xsl:template match="content-style[not(preceding-sibling::content-style)]" />

<xsl:template match="page[following-sibling::content-style and not(preceding-sibling::content-style)]" />
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24015026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档