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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
Xml 使用XPath选择只包含一个属性的元素_Xml_Xpath - Fatal编程技术网

Xml 使用XPath选择只包含一个属性的元素

Xml 使用XPath选择只包含一个属性的元素,xml,xpath,Xml,Xpath,我希望在XML文件中只使用XPath获取只包含一个属性的元素 这是XML文件: <?xml version="1.0" encoding="UTF-8"?> <livre titre="Mon livre"> <auteurs> <auteur nom="nom1" prenom="prenom1" /> <auteur nom="nom2" prenom="prenom2" /> <

我希望在XML文件中只使用XPath获取只包含一个属性的元素

这是XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<livre titre="Mon livre">
    <auteurs>
        <auteur nom="nom1" prenom="prenom1" />
        <auteur nom="nom2" prenom="prenom2" />
    </auteurs>
    <sections>
        <section titre="Section1">
            <chapitre titre="Chapitre1">
                <paragraphe>Premier paragraphe</paragraphe>
                <paragraphe>Deuxième paragraphe</paragraphe>
            </chapitre>
            <chapitre titre="Chapitre2">
                <paragraphe>Premier paragraphe</paragraphe>
            </chapitre>
        </section>
        <section titre="section2">
            <chapitre titre="Chapitre1">
                <paragraphe>Premier paragraphe</paragraphe>
                <paragraphe>Deuxième paragraphe</paragraphe>
            </chapitre>
        </section>
    </sections>
</livre>
但它不起作用

但是我编写的XPath查询选择包含多个属性的所有元素,我只想选择只包含一个属性的元素

XPath查询的结果应如下所示:

Description: titre="Mon livre"
XPath location: /livre[1]
Start location: 2:1
End location: 20:9

Description: titre="Section1"
XPath location: /livre[1]/sections[1]/section[1]
Start location: 8:5
End location: 16:19

Description: titre="Chapitre1"
XPath location: /livre[1]/sections[1]/section[1]/chapitre[1]
Start location: 9:13
End location: 12:24

Description: titre="Chapitre2"
XPath location: /livre[1]/sections[1]/section[1]/chapitre[2]
Start location: 13:13
End location: 15:24

Description: titre="section2"
XPath location: /livre[1]/sections[1]/section[2]
Start location: 17:9
End location: 18:19
我该怎么做呢?

这应该可以:

//*[count(@*)=1]
p、 在美国,我得到了6个项目,对于这些数据,在
第2节下有另一个
章节

这应该有效:

//*[count(@*)=1]
p、 在美国,我得到了6个项目,对于这些数据,在
第2节下有另一个
章节

这应该有效:

//*[count(@*)=1]
p、 在美国,我得到了6个项目,对于这些数据,在
第2节下有另一个
章节

这应该有效:

//*[count(@*)=1]
p、 在美国,我得到了6个项目,对于这些数据,在
第2节下还有另一个
章节