【问题标题】:XSL select text in parent node with all children except one specific child nodeXSL 在父节点中选择除一个特定子节点外的所有子节点中的文本
【发布时间】:2014-04-15 15:03:12
【问题描述】:

我对 XML 和 XSL 有疑问, 这是我的 XML 文档的一部分:

<node1>
    <node2>
        <node3>text inside node3 I don't want select</node3>
        text inside node2 that I want select
        <node4> text in node4 I want select</node4>
    </node2>
</node1>

node2 里面有一些文本和 2 个子节点(node3 和 node4),我需要选择 node2 中包含的文本和子节点 4 中的文本,除了那个 在节点 3。 我还说node2的结构并不总是相同的,有时它只有一些文本,或者可能有一些带有node3和/或node4的文本。 此外,node2 中元素的顺序可能并不总是相同,但结果的顺序应该与 XML 中的顺序相同。

感谢您的帮助。

【问题讨论】:

    标签: xml xslt select


    【解决方案1】:

    node2 上下文开始,您可以使用除node3 中的那些之外的所有后代文本节点

    .//text()[not(parent::node3)]
    

    或者更一般地说,如果node3 可能有子元素

    <node2>
       keep this
       <node3>drop this <node7>drop this too</node7></node3>
       <node4>keep this</node4>
    </node2>
    

    然后使用ancestor:: instead ofparent::`

    .//text()[not(ancestor::node3)]
    

    【讨论】:

    • 我已尝试测试您的第一个解决方案,但似乎无法正常工作。就像在我的示例中一样,它选择 node2 内的文本,但不选择 node4 内的文本。
    • 我也测试了第二种解决方案,但它显示了同样的问题。
    • @GiulioBambini 这取决于你所说的“不选择”是什么意思 - 请记住,这个表达式将返回一个 set 文本节点,并且(在 XSLT 1.0 中)如果你尝试使用value-of 一个节点集,你得到的只是集合中按文档顺序排列的 first 节点的字符串值,而忽略集合中的其余节点。要获得 all 的值,您通常会使用 apply-templatescopy-of 而不是 value-of,例如&lt;xsl:apply-templates select=".//text()[not(ancestor::node3)]"/&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多