Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以使用XPath获取此结果集?_Javascript_Xml_Xpath - Fatal编程技术网

Javascript 是否可以使用XPath获取此结果集?

Javascript 是否可以使用XPath获取此结果集?,javascript,xml,xpath,Javascript,Xml,Xpath,我有一些XML,我正在使用javascript和XPath(1.0)作为学习过程。我能处理基本的事情,但把所有的事情放在一起就是让我把头发拔出来 我的XML文件包含多本这样格式的书 <bookstore> <!-- Added by edit --> <book category="Fantasy &amp; Adventure" cover="paperback" released="true" special="true" homepag

我有一些XML,我正在使用javascript和XPath(1.0)作为学习过程。我能处理基本的事情,但把所有的事情放在一起就是让我把头发拔出来

我的XML文件包含多本这样格式的书

<bookstore>     <!-- Added by edit -->
    <book category="Fantasy &amp; Adventure" cover="paperback" released="true" special="true" homepage="true">
        <title>Belgariad 1: Pawn of Prophecy</title>
        <artwork>david-eddings-belgariad-1.jpg</artwork>
        <author>David Eddings</author>
        <year>2006</year>
        <price>7.99</price>
        <rating>
            <score>4</score>
            <amount>340</amount>
        </rating>
        <description>
            <short>An ancient prophecy &amp; and a maimed God...--nl--Long ago, the evil God Torak fought a war to obtain an object of immense power - the Orb of Aldur.But Torak was defeated and the Orb reclaimed by Belgarath the sorcerer.</short>
            <long>Garion, a young farm lad, loves the story when he first hears it from the old storyteller. But it has nothing to do with him. Or does it? For the stories also tell of a prophecy that must be fulfilled - a destiny handed down through the generations.--nl--And Torak is stirring again...</long>
        </description>
        <reviews>
            <review>
                <source>Anne McCaffrey</source>
                <text>Fabulous.</text>
            </review>
            <review>
                <source>Darren Shan</source>
                <text>Fun, exciting, intriguing fantasy in which the characters are as important as the quest and magical elements... immerse yourself and enjoy!</text>
            </review>
        </reviews>
    </book>
</bookstore>
这使我按顺序获得了这3个元素(标题、艺术品、价格;标题、艺术品、价格等),但如果我添加self::description/short或child::*/short之类的内容,我就不会得到任何关于描述的返回


最糟糕的情况是,我可以单独访问所有部分。

如果元素名称是唯一的,您可以对
子代或self::
轴使用这种简单的方法:

/bookstore/book[@special='true' and @homepage='true']/descendant-or-self::*[self::title | self::artwork | self::price | self::short | self::score | self::amount]
其产出是:

Belgariad 1: Pawn of Prophecy 
david-eddings-belgariad-1.jpg 
7.99 
4 
340 
An ancient prophecy &amp; and a maimed God...--nl--Long ago, the evil God Torak fought a war to obtain an object of immense power - the Orb of Aldur.But Torak was defeated and the Orb reclaimed by Belgarath the sorcerer.

如果它们不是唯一的,则需要在谓词中使用
parent::
轴指定父项:

/bookstore/book[@special='true' and @homepage='true']/descendant-or-self::*[self::title | self::artwork | self::price | self::short[parent::description] | self::score[parent::rating] | self::amount[parent::rating]]
本例中的输出应相同


请注意,输出的顺序是源文件顺序,而不是XPath表达式的顺序。

如果您的XPath版本支持它,则可以使用较短的表达式:

/bookstore/book[@special='true'][@homepage='true']/(title, artwork, price, //short, //score, //amount)

是的,就是这样。谢谢!对于最后3个项目,我尝试了genderant或self::,但我没有想到在谓词之前使用它。一个如此接近但迄今为止的案例:(我认为我遇到的问题有一半是因为它是1.0版本,所以任何带有括号的东西,比如你的建议,最终都会出现错误。我确实为2.0或更高版本提出了一些解决方案,这些解决方案在XPath测试网站上有效,但我自己不会。
/bookstore/book[@special='true'][@homepage='true']/(title, artwork, price, //short, //score, //amount)