-1

这是我的 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>

这是我的完整代码:-

<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">它这样的
属性值如果删除这个属性值然后工作完美的话会有什么问题......
任何想法它的食物对我有帮助......
谢谢

4

2 回答 2

1

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

于 2013-03-26T07:50:04.747 回答
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>
于 2013-03-26T11:29:56.980 回答