如何为每个所需节点检索两个属性

时间:2017-02-15 17:02:39

标签: xpath

我正在使用Google Sheet阅读XML数据。 不幸的是,我正在努力正确地提取XML。 https://www.w3schools.com/xml/xpath_syntax.asp,SO和其他谷歌搜索的消息来源没有帮助。

处理下面的简化数据时

<item>
    <profile_url>www.profile1.com</profile_url>
    <answers>
        <answers_item>
            <answer>answer text 1.1</answer>
        </answers_item>
        <answers_item>
            <answer>answer text 1.2</answer>
        </answers_item>
    </answers>
<item>
<item>
    <profile_url>www.profile2.com</profile_url>
    <answers>
        <answers_item>
            <answer>answer text 2.1</answer>
        </answers_item>
        <answers_item>
            <answer>answer text 2.2</answer>
        </answers_item>
    </answers>
<item>

输出需要采用以下格式,因此电子表格中不需要额外的格式

www.profile1.com   answer text 1.1   answer text 1.2
www.profile2.com   answer text 2.1   answer text 2.2

变种试图并且未能给出正确的输出

"//profile_url //answers/answers_item/answer"
"//profile_url | //answers/answers_item/answer"

提前致谢 罗布

尝试了suggested duplicate

的修复程序

1)使用字符串连接

string-join(//item/(concat(profile_url/text(), '.', answers/answers_item/answer//text())), "&#10;")

发出以下错误(可能是因为使用的XPath不是2.0)

Imported Xml content can not be parsed.

2)使用

concat(//profile_url/text(), " ", //answers/answers_item/answer/text()) 

仅提供第一个条目。

1 个答案:

答案 0 :(得分:0)

假设每个GET中只有两个answers_item,则以下xpath

item

应该提供所需的输出:

第一个concat(//item[1]/profile_url, " ", //item[1]/answers/answers_item[1]/answer, " " , //item[1]/answers/answers_item[2]/answer)

www.profile1.com answer text 1.1 answer text 1.2

对于第二个,您需要将item修改为item[1]

如果您使用的是Java,这可以轻松完成。

相关问题