代码之家  ›  专栏  ›  技术社区  ›  robjmills

Xpath路径帮助

  •  1
  • robjmills  · 技术社区  · 15 年前

    我有以下XML:

    <products>
        <product>       
            <name>Flat Panel Monitor</name>
            <code>LS123</code>
            <attributes>
                <attribute>
                    <group>Color</group>
                    <values>
                        <value>Red</value>
                        <value>Blue</value>
                        <value>Green</value>
                    </values>
                </attribute>
                <attribute>
                    <group>Warranty</group>
                    <values>
                        <value>5 Year</value>
                    </values>
                </attribute>
            </attributes>
        </product>
    </products>
    

    /product/attributes/attribute/values/value 将返回保修组中的所有价值,包括价值,但我需要将它们分开。

    我猜在伪代码中我要说的是获取所有“值”,其中父节点“值”是节点“组”的兄弟节点,值为“Color”-希望这是可能的?

    4 回复  |  直到 15 年前
        1
  •  4
  •   Paul Butcher    15 年前

    您需要使用方括号来过滤节点,因此: /product/attributes/attribute[group='Color']/values/value

        2
  •  3
  •   AakashM    15 年前

    value 具有 group 的“叔叔” Color ,因此将其作为您已经计算出的xpath的一个条件:

    /product/attributes/attribute/values/value[../../group = 'Color']
    

    价值 颜色

        3
  •  1
  •   Daniel Haley    15 年前

    还有一个选择:

    //value[ancestor::attribute[group='Color']]

        4
  •  0
  •   Artefacto    15 年前
    //values[preceding-sibling::group[text()="Color"]]/value
    
    推荐文章