【问题标题】:XPath to select XML node without a child node with a certain value?XPath 选择没有具有特定值的子节点的 XML 节点?
【发布时间】:2016-11-11 23:31:14
【问题描述】:

我有以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<targets>
    <target sanctions-set-id="4387" ssid="5762">
        <individual>
            <identity ssid="5764" main="true">
                <name ssid="27036" name-type="primary-name">
                    <value>SomeName1</value>
                </name>
            </identity>
        </individual>
        <modification modification-type="de-listed" enactment-date="2016-02-29" publication-date="2016-03-01" effective-date="2016-03-01"/>
        <modification modification-type="amended" enactment-date="2015-11-17" publication-date="2015-11-18" effective-date="2015-11-18"/>
        <modification modification-type="amended" enactment-date="2014-11-27" publication-date="2014-11-28" effective-date="2014-11-28"/>
        <modification modification-type="amended" enactment-date="2013-12-18" publication-date="2013-12-19" effective-date="2013-12-20"/>
        <modification modification-type="listed"/>
    </target>
    <target sanctions-set-id="4388" ssid="5763">
        <individual>
            <identity ssid="5765" main="true">
                <name ssid="27037" name-type="primary-name">
                    <value>SomeName2</value>
                </name>
            </identity>
        </individual>
        <modification modification-type="amended" enactment-date="2015-11-17" publication-date="2015-11-18" effective-date="2015-11-18"/>
        <modification modification-type="amended" enactment-date="2014-11-27" publication-date="2014-11-28" effective-date="2014-11-28"/>
        <modification modification-type="amended" enactment-date="2013-12-18" publication-date="2013-12-19" effective-date="2013-12-20"/>
        <modification modification-type="listed"/>
    </target>   
</targets>

我需要选择任何没有删除修改类型子节点的目标节点。换句话说,我的 xpath 应该只返回 SomeName2 的第二个目标,因为没有删除的修改类型。

这是我能得到的最接近的,但它仍然选择两者。

targets/target[modification[@modification-type != 'de-listed']]

【问题讨论】:

    标签: xml xpath


    【解决方案1】:

    您的 XPath 选择两者的原因是因为两者都具有modification 元素,其@modification-type 属性等于de-listed 以外的值。使用not() 而不是!=...

    这个 XPath,

    //target[not(modification[@modification-type = 'de-listed'])]
    

    将选择所有具有no modification 子元素且modification-type 属性等于de-listedtarget 元素。

    【讨论】:

    • 正是我需要的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 2014-07-06
    相关资源
    最近更新 更多