xquery:从父级中选择几个级别的祖先

时间:2014-03-19 01:51:52

标签: xquery ancestor

我正在尝试选择< li>的值父节点之前的祖先节点。以下是文档示例im-trg.xml:

<trg>
<category>
    <h2>Accounting and Auditing</h2>
    <ul>
    <li>Laws and Regulations
        <ul>
            <li><a href="url1">Regulation S-X</a></li>
        </ul>
    </li>
    <li>Staff Guidance
        <ul>
            <li>No Action Letters
                <ul>
                    <li><a href="url2">Robert Van Grover, Esq., Seward and Kissel LLP</a> (November 5, 2013)</li>
                </ul>
            </li>
        </ul>
    </li>
    </ul>
</category>
</trg>

这是我的问题:

for $x in doc("C:\im-trg.xml")//li/a
return 
<item>
<title>{data($x)}</title>
<documentType>{data($x/ancestor::li[2])}</documentType>
<category>{data($x/ancestor::category/h2)}</category>
</item>

我得到了:

<item>
  <title>Regulation S-X</title>
  <documentType>Laws and RegulationsRegulation S-X</documentType>
  <category>Accounting and Auditing</category>
</item>

对于&lt; documentType&gt;,我想只选择祖先&lt; li&gt;紧接在&lt; li&gt;之前&lt; a&gt;的父级,表示文档的类型,所以我想:

<item>
  <title>Regulation S-X</title>
  <documentType>Laws and Regulations</documentType>
  <category>Accounting and Auditing</category>
</item>

<item>
  <title>Robert Van Grover, Esq., Seward and Kissel LLP</title>
  <documentType>No Action Letters</documentType>
  <category>Accounting and Auditing</category>
</item>

我不认为我可以从根目录下来,因为父母&lt; li&gt;有时是双嵌套,有时是三重嵌套。

1 个答案:

答案 0 :(得分:0)

元素的文本值是 all 其文本节点后代的串联。如果您只想要元素立即包含的文本,则应明确选择其文本子项,例如

data($x/ancestor::li[2]/text())