首页 文章

XPath,ID与链接属性匹配的元素的返回值和第二个元素的其他属性?

提问于
浏览
2

我是一名使用XPath的初学者,如果另一个元素的属性类型= X AND属性链接与第一个元素的ID匹配,则需要完成一个返回XML元素值的任务 . 元素有不同的父母(这些父母是彼此的兄弟姐妹) .

XML示例:

<orders>
<order orderID="o1">
    <shipfrom>
        <company>
            <name>XYZ Company</name>
            <address>
                <addressline1>Building 1</addressline1>
                <addressline2>Floor 3</addressline2>
                <addressline3>Company Street</addressline3>
                <city>Cork</city>
                <country>Ireland</country>
                <postcode>XYZ123</postcode>
            </address>
        </company>
    </shipfrom>
    <shipto>
        <person>
            <firstname>Mary</firstname>
            <lastname>O'Brien</lastname>
            <emailaddress>maryobrien@email.ie</emailaddress>
        </person>
        <address>
            <addressline1>12 House Estate</addressline1>
            <addressline2>Estate Road</addressline2>
            <addressline3></addressline3>
            <city>Dublin</city>
            <country>Ireland</country>
            <postcode>D41234</postcode>
        </address>
    </shipto>
    <items>
        <item itemID="i1">
            <description>Blue Biro</description>
            <quantity>20</quantity>
            <price>0.10</price>
            <code>BIROBLU</code>
        </item>
        <item itemID="i2">
            <description>Black Biro</description>
            <quantity>20</quantity>
            <price>0.10</price>
            <code>BIROBLA</code>
        </item>
    </items>
    <payments>
        <payment type="cash">
            <amount>2.00</amount>
        </payment>
        <payment type="cash">
            <amount>2.00</amount>
        </payment>
    </payments>
    <history>
        <status link="i1" type="outofstock">
            <date>
                <day>08</day>
                <month>10</month>
                <year>2016</year>
            </date>
        </status>
        <status link="i2" type="dispatched">
            <date>
                <day>08</day>
                <month>10</month>
                <year>2016</year>
            </date>
        </status>
    </history>
</order>

任务是列出具有特定状态的所有项目详细信息 .

目前我有这个:

//items[following-sibling::history/status[@type='dispatched']]/item

但是,它会返回订单中的所有项目,即使其中只有一项属于列出的状态 .

我假设我需要创建一个附加条件 - 所以如果属性类型是'dispatched'并且链接等于itemID,则返回详细信息 .

但我不知道该怎么做!有人可以帮忙吗?

1 回答

  • 1

    您可能需要使用item作为上下文节点而不是项目,然后您可以使用link将结果限制为当前itemID;这应该工作:

    //item[../following-sibling::history/status[@type='dispatched']/@link=@itemID]
    

相关问题