如果根元素具有名称空间`xmlns`,则无法工作

时间:2013-03-26 07:19:16

标签: javascript html xml xslt xpath

这是我的xml文件: -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<results xmlns="http://gisgraphy.com">
    <result>
        <distance>1071.59873299109</distance>
        <name>Irungattukottai</name>
        <adm1Code>25</adm1Code>
        <adm1Name>State of Tamil Nadu</adm1Name>
        <asciiName>Irungattukottai</asciiName>
        <countryCode>IN</countryCode>
        <featureClass>P</featureClass>
        <featureCode>PPL</featureCode>
        <featureId>1269687</featureId>
        <gtopo30>47</gtopo30>
        <population>0</population>
        <timezone>Asia/Kolkata</timezone>
        <lat>12.996390342712402</lat>
        <lng>79.99082946777344</lng>
        <placeType>City</placeType>
        <oneWay>false</oneWay>
        <length>0.0</length>
        <google_map_url>http://maps.google.com/maps?f=q&amp;amp;ie=UTF-8&amp;amp;iwloc=addr&amp;amp;om=1&amp;amp;z=12&amp;amp;q=Irungattukottai&amp;amp;ll=13.026390342712402,79.99082946777344</google_map_url>
        <yahoo_map_url>http://maps.yahoo.com/broadband?mag=6&amp;amp;mvt=m&amp;amp;lon=79.99082946777344&amp;amp;lat=12.996390342712402</yahoo_map_url>
        <country_flag_url>/images/flags/IN.png</country_flag_url>
    </result>
    <result>
        <distance>1892.33578431928</distance>
        <name>Valapuram</name>
        <adm1Code>25</adm1Code>
        <adm1Name>State of Tamil Nadu</adm1Name>
        <asciiName>Valapuram</asciiName>
        <countryCode>IN</countryCode>
        <featureClass>P</featureClass>
        <featureCode>PPL</featureCode>
        <featureId>1466104</featureId>
        <gtopo30>33</gtopo30>
        <population>0</population>
        <timezone>Asia/Kolkata</timezone>
        <lat>13.016940116882324</lat>
        <lng>80.00167083740234</lng>
        <placeType>City</placeType>
        <oneWay>false</oneWay>
        <length>0.0</length>
        <google_map_url>http://maps.google.com/maps?f=q&amp;amp;ie=UTF-8&amp;amp;iwloc=addr&amp;amp;om=1&amp;amp;z=12&amp;amp;q=Valapuram&amp;amp;ll=13.046940116882324,80.00167083740234</google_map_url>
        <yahoo_map_url>http://maps.yahoo.com/broadband?mag=6&amp;amp;mvt=m&amp;amp;lon=80.00167083740234&amp;amp;lat=13.016940116882324</yahoo_map_url>
        <country_flag_url>/images/flags/IN.png</country_flag_url>   
    </result>
</results>

这是我的complite代码: -

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        var xml;
        $.get(
        "search.xml",
        null,
        function (data) {
            xml = data;
        },
        "xml"
    );

        function get_list(ls) {
            var elName = $('#select').val();
            var value = $('#value').val();
            if(elName == "" || value == "")
            {
            }
            else if (elName != "" || value != "")
            {
            //var xPath = '//loction[ @lat '+ ls +' "'+elName+'" and @lng '+ ls +' "'+value+'"][1]/preceding-sibling::*/@*';
            var xPath = '//result[lat='+elName+' and lng='+value+']/node()/text()';
            //var xPathh = '//lng[@lng '+ ls +' "'+ value +'"]'+'/../City/@value';

            var iterator = xml.evaluate(xPath, xml.documentElement, null,
                XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
            var thisNode = iterator.iterateNext();
            var str = '';
            while (thisNode) {
                if (str) {
                    str += ', ';
                }
                str += thisNode.textContent;
                thisNode = iterator.iterateNext();
            }

            $("#result").text(str);
        }
        else 
        {}
        }
    </script>
</head>
<body>
    <input type="text" id="select">
    <input type="text" id="value">
    <input type="button" name="button" value="Search" onclick="get_list('=')">
    <div id="result">
    </div>
</body>
</html>


  i want:-

我想在文本框中输入纬度和经度值,如果两者在我的xml文件中匹配则返回兄弟节点值。

problem:-

我不知道如果根元素具有属性值
是什么问题  比如<results xmlns="http://gisgraphy.com">它不起作用  如果删除此属性值,那么工作完美...
任何想法,这对我的食物帮助...
感谢

2 个答案:

答案 0 :(得分:1)

你需要一个稍微不同的xpath,其中“result”有一个前缀,然后使用命名空间解析器作为evaluate函数的第三个参数。请参阅https://developer.mozilla.org/en-US/docs/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver以获取有关如何执行此操作的说明和示例。

答案 1 :(得分:1)

xslt4convert: -

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" version="1.0" encoding="UTF-8" />
        <xsl:template match="*">
                <xsl:element name="{local-name()}">
                        <xsl:apply-templates select="@* | node()"/>
                </xsl:element>
        </xsl:template>
</xsl:stylesheet>

创建一个php文件来加载xml和xslt文件: -

<?php
$sourcedoc = new DOMDocument();
    $sourcedoc->load('filename.xml');
    $stylesheet = new DOMDocument();
    $stylesheet->load('new4convert.xsl');
     // create a new XSLT processor and load the stylesheet
    $xsltprocessor = new XSLTProcessor();
    $xsltprocessor->importStylesheet($stylesheet);

    // save the new xml file
    file_put_contents('filename.xml', $xsltprocessor->transformToXML($sourcedoc));
?> 

它的声音是返回这种类型的xml文件: -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <results>
        <result>
            <distance>1071.59873299109</distance>
            <name>Irungattukottai</name>
            <adm1Code>25</adm1Code>
            <adm1Name>State of Tamil Nadu</adm1Name>
            <asciiName>Irungattukottai</asciiName>
            <countryCode>IN</countryCode>
            <featureClass>P</featureClass>
            <featureCode>PPL</featureCode>
            <featureId>1269687</featureId>
            <gtopo30>47</gtopo30>
            <population>0</population>
            <timezone>Asia/Kolkata</timezone>
            <lat>12.996390342712402</lat>
            <lng>79.99082946777344</lng>
            <placeType>City</placeType>
            <oneWay>false</oneWay>
            <length>0.0</length>
            <google_map_url>http://maps.google.com/maps?f=q&amp;amp;ie=UTF-8&amp;amp;iwloc=addr&amp;amp;om=1&amp;amp;z=12&amp;amp;q=Irungattukottai&amp;amp;ll=13.026390342712402,79.99082946777344</google_map_url>
            <yahoo_map_url>http://maps.yahoo.com/broadband?mag=6&amp;amp;mvt=m&amp;amp;lon=79.99082946777344&amp;amp;lat=12.996390342712402</yahoo_map_url>
            <country_flag_url>/images/flags/IN.png</country_flag_url>
        </result>
        <result>
            <distance>1892.33578431928</distance>
            <name>Valapuram</name>
            <adm1Code>25</adm1Code>
            <adm1Name>State of Tamil Nadu</adm1Name>
            <asciiName>Valapuram</asciiName>
            <countryCode>IN</countryCode>
            <featureClass>P</featureClass>
            <featureCode>PPL</featureCode>
            <featureId>1466104</featureId>
            <gtopo30>33</gtopo30>
            <population>0</population>
            <timezone>Asia/Kolkata</timezone>
            <lat>13.016940116882324</lat>
            <lng>80.00167083740234</lng>
            <placeType>City</placeType>
            <oneWay>false</oneWay>
            <length>0.0</length>
            <google_map_url>http://maps.google.com/maps?f=q&amp;amp;ie=UTF-8&amp;amp;iwloc=addr&amp;amp;om=1&amp;amp;z=12&amp;amp;q=Valapuram&amp;amp;ll=13.046940116882324,80.00167083740234</google_map_url>
            <yahoo_map_url>http://maps.yahoo.com/broadband?mag=6&amp;amp;mvt=m&amp;amp;lon=80.00167083740234&amp;amp;lat=13.016940116882324</yahoo_map_url>
            <country_flag_url>/images/flags/IN.png</country_flag_url>   
        </result>
    </results>