如何按标记属性检索XML标记

如何按标记属性检索XML标记,xml,Xml,我正在努力处理javascript中的一些XML,如何才能只选择具有特定属性值的标记 中的以下示例使用了此选项: 日常意大利语 吉娅达·德·劳伦蒂斯 2005 30 哈利·波特 J K.罗琳 2005 29.99 XQuery启动 詹姆斯·麦戈文 伯特纳 库尔特·卡格尔 詹姆斯·林恩 瓦迪亚纳坦·纳加拉扬 2003 49.99 学习XML 埃里克·T·雷 2003 39.95 然后下面的脚本获取并在新行上打印来自价格标签的所有文本 <html> <body>

我正在努力处理javascript中的一些XML,如何才能只选择具有特定属性值的标记

中的以下示例使用了此选项:


日常意大利语
吉娅达·德·劳伦蒂斯
2005
30
哈利·波特
J K.罗琳
2005
29.99
XQuery启动
詹姆斯·麦戈文
伯特纳
库尔特·卡格尔
詹姆斯·林恩
瓦迪亚纳坦·纳加拉扬
2003
49.99
学习XML
埃里克·T·雷
2003
39.95
然后下面的脚本获取并在新行上打印来自价格标签的所有文本

<html>
<body>
    <script type="text/javascript">
        function loadXMLDoc(dname){
            if (window.XMLHttpRequest){
                xhttp=new XMLHttpRequest();
            }
            else{
                xhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xhttp.open("GET",dname,false);
            xhttp.send("");
            return xhttp.responseXML;
        }
        //getting the xml into a var to parse       
        xml=loadXMLDoc("books.xml");


        //the path in question
        path="/bookstore/book/price/text()"   ////how can i change this to only select the prices of the books where the category="COOKING" ?


        // code for IE
        if (window.ActiveXObject){
            var nodes=xml.selectNodes(path);

            for (i=0;i<nodes.length;i++)
                {
                    document.write(nodes[i].nodeValue);
                    document.write("<br />");
                }
        }
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument){
            var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE,null);
            var result=nodes.iterateNext();

            while (result)
                {
                    document.write(result.nodeValue + "<br />");
                    result=nodes.iterateNext();
                }
        }
    </script>
</body>
</html>

函数loadXMLDoc(dname){
if(window.XMLHttpRequest){
xhttp=newXMLHttpRequest();
}
否则{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”、dname、false);
xhttp.send(“”);
返回xhttp.responseXML;
}
//将xml放入要解析的变量中
xml=loadXMLDoc(“books.xml”);
//正在讨论的道路
path=“/bookstore/book/price/text()”///如何更改此选项以仅选择category=“COOKING”所在书籍的价格?
//IE代码
if(window.ActiveXObject){
var nodes=xml.selectNodes(路径);

对于(i=0;i在您的路径中,您需要放置例如
//bookName/[@CATEGORY=COOKING]
的@是它查看属性的地方。希望这对您有所帮助

<html>
<body>
    <script type="text/javascript">
        function loadXMLDoc(dname){
            if (window.XMLHttpRequest){
                xhttp=new XMLHttpRequest();
            }
            else{
                xhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xhttp.open("GET",dname,false);
            xhttp.send("");
            return xhttp.responseXML;
        }
        //getting the xml into a var to parse       
        xml=loadXMLDoc("books.xml");


        //the path in question
        path="/bookstore/book/price/text()"   ////how can i change this to only select the prices of the books where the category="COOKING" ?


        // code for IE
        if (window.ActiveXObject){
            var nodes=xml.selectNodes(path);

            for (i=0;i<nodes.length;i++)
                {
                    document.write(nodes[i].nodeValue);
                    document.write("<br />");
                }
        }
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument){
            var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE,null);
            var result=nodes.iterateNext();

            while (result)
                {
                    document.write(result.nodeValue + "<br />");
                    result=nodes.iterateNext();
                }
        }
    </script>
</body>
</html>