【问题标题】:sum of parent child and parent nodes父子节点和父节点之和
【发布时间】:2013-06-10 22:34:25
【问题描述】:

我想对我的 xslt 做一个小的改动,如下所示。

 <xsl:variable name="sectnum">
    <xsl:number level="any" count="section[(@num and @level ='sect2') or (@level !='sect2' and @level !='sect1') or @level='sect2' or contains(//content-style,'Part')]"/>
        </xsl:variable>

这里实际上我有 para 作为 section/para 的路径,在 para 中我有 para/content-style。 我想用包含'Part'的字符串来计算para。请让我知道我该怎么做。上面的变量只为@level 属性提供计数,而不是为 para 部分。

我的xml的一部分如下。

  <?xml version="1.0" encoding="UTF-8"?>
<chapter>
<section level="sect2" number-type="manual" num="1.">
            <title>INTRODUCTION</title>
            <para>
                <phrase>1.001</phrase> This part will consider matters relevant to the administration of a deceased's estate from the death to the application for the grant. Chapter 1 will outline some preliminary steps to be taken immediately on obtaining instructions to act. The distinction between testate and intestate succession, and the practical implications of each, will be considered in Chapter 2. Foreign elements, particularly the relevant questions of domicile, the involvement of PRC law, and overseas property, will be noted in Chapter 3. The steps necessary to obtain estate duty clearance (where now necessary) will be briefly considered in Chapter 4. </para>
                </section>
                <section level="sect2">
                    <para>
                    <content-style font-style="bold">Part 1 The deceased person</content-style>
                </para>
                </section>
                </chapter>

第二个场景

    <chapter><section level="sect2">
    <para>
                        <content-style font-style="bold">Part 5 The dependants</content-style>
                    </para></section>
<section level="sect2">
                    <para>Complete the table at Appendix B. </para>
                    <para>
                        <content-style font-style="bold">Part 6 The estate</content-style>
                    </para></section>
    </chapter>

谢谢

【问题讨论】:

  • 如果您添加一个输入示例,这将非常有帮助。你会只计算不同条件的部分吗?
  • 嗨@hr_117 这里的计数是基于部分条件的。并且还应该对第二部分进行计数,因为它在段落中有“部分”(第 1 部分死者)。谢谢
  • 您的新示例不包含任何 section 元素。您是在计算 section 元素还是 para 元素?如果您在这两种情况下都显示了预期的输出,那将会有所帮助。谢谢!
  • @TimC 抱歉输入无效。现在我更新了我的问题。再次抱歉。谢谢

标签: xslt


【解决方案1】:

看你当前的变量,除非你有错别字,其实可以简化成下面这样

<xsl:number level="any" count="section[@level !='sect1' or contains(//content-style,'Part')]"/>

但是,我认为您遇到的问题之一是包含。因为您的 xpath 表达式以 // 开头,这意味着它与顶级文档元素相关,而不是当前节点,因此只会检查它找到的第一个 content-style 元素。

另外,如果您只想将 section 元素与 content-style 包含“Part”的 para 元素一起计算,那么您必须像这样扩展它

"... or contains(.//content-style,'Part')] or para[not(.//content-style)]"

注意 contains 开头的句号表示它是相对于当前节点的。

尝试其中任何一种方法

<xsl:number level="any" 
     count="section[
        (@num and @level ='sect2') 
        or (@level !='sect2' and @level !='sect1') 
        or @level='sect2' 
        or para[not(.//content-style)] 
        or contains(.//content-style,'Part')]"/>

或者

<xsl:number level="any" 
     count="section[
         @level !='sect1' 
         or para[not(.//content-style)]  
         or contains(.//content-style,'Part')]"/>

注意contains开头的句号表示它是相对于当前节点的。

【讨论】:

  • 嗨@Tim C 这很好用,但也有一个例外,如果它像下面这样就不应该计算在内 第 1 部分 死者。即如果内容样式在文本内。请让我知道我该怎么做。再次感谢
  • 当我将它添加到我的变量时,当我将它添加到我更新的问题(第二种情况)中的数据时,节号会重复,它给出的是 sec-01,sec-01,但我想要它是 sec-01 和 sec-02。请帮我解决这个问题。谢谢。
  • 嗯...这很奇怪,因为编号为 1、2。我不确定我错过了什么,恐怕。
  • @user2423959:Tim C 的解决方案对我来说很好用。我害怕帮助你更多,我们需要一个新问题。有一个小的完整的不工作示例(xml 和 xlst)
  • 嗨@hr_117 但是当我尝试它时有重复的数字,我使用altova xml spy。否则请您提供您的电子邮件地址,以便我可以通过电子邮件将代码发送给您,因为我无法在此页面中输入我的整个代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
相关资源
最近更新 更多