Select node based on sibling's value

和自甴很熟 提交于 2021-01-28 16:17:42

问题


I need XPath syntax (for use in simplexml) for searching the contents of the LayoutPosNo element that matches exactly, say, the number 1001 and returning the text in the sibling Descrip element. The LayoutPosNo's are all unique, so I only need the first match.

Here is the structure of the XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <record>
        <LayoutPosNo>10</LayoutPosNo>
        <Descrip>This is the red room</Descrip>
    </record>
    <record>
        <LayoutPosNo>993</LayoutPosNo>
        <Descrip>This is the yellow room</Descrip>
    </record>
    <record>
        <LayoutPosNo>1001</LayoutPosNo>
        <Descrip>This is the purple room</Descrip>
    </record>
</data-set>

回答1:


The following XPath

/data-set/record[LayoutPosNo = 1001]/Descrip/text()

will select

This is the purple room

as requested.



来源:https://stackoverflow.com/questions/31117609/select-node-based-on-siblings-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!