CF18 中的 XmlSearch() 函数未返回预期结果?

XmlSearch() function in CF18 not returning expected result?

伙计们,我在 CF18 服务器上工作并面临 XmlSearch 函数问题。 我的 XML 如下所示

我正在尝试在此处搜索多边形节点。我尝试了多种方法,但它 returns 为空数组。我不确定我的方法有什么问题。

<cfdump var="#isXML(parsedXML)#">    // return YES

<cfset selectedElements = XmlSearch(parsedXML,'/MultiSurface/surfaceMembers/Polygon')>
<cfdump var="#selectedElements#">  //Empty Array

<cfdump var="#XmlSearch(parsedXML, '//MultiSurface')#">   //Empty Array
<cfdump var="#XmlSearch(parsedXML, 'surfaceMembers')#">   //Empty Array
<cfdump var="#XmlSearch(parsedXML, '//surfaceMembers')#"> //Empty Array

任何人都可以帮助我更正此代码。

已更新:

XML数据

<MultiSurface xmlns="http://www.opengis.net/gml">
  <surfaceMembers>
    <Polygon>
      <exterior>
        <LinearRing>
          <posList>41.710227999483223 -82.835759999441933 41.708971 -82.835118 41.708650999999996 -82.834508 41.708527 -82.833728999999991 41.708445 -82.833073 41.708635 -82.831451 41.709134 -82.830407 41.709300999999996 -82.829388999999992 41.709134 -82.828593 41.707156999999995 -82.823673 41.706951 -82.822746999999993 41.707038999999995 -82.819758 41.707347999999996 -82.816135 41.707451999999996 -82.81589799999999 41.707314 -82.815269 41.707381999999996 -82.814407 41.707215999999995 -82.813154 41.707225 -82.812176 41.707676 -82.811157999999992 41.707851999999995 -82.810531 41.707871 -82.810153 41.706157999999995 -82.807203 41.706325000248334 -82.806863999719781 41.706559999999996 -82.806877 41.708146 -82.808756 41.708321999513188 -82.808861000175852 41.708498 -82.808678 41.708772000261668 -82.808638998629959 41.709143999999995 -82.808874 41.709770999999996 -82.809592 41.713989999999995 -82.811667 41.715095999999996 -82.811771 41.715654 -82.811954 41.717280998996912 -82.811921999770249 41.717876 -82.811745 41.718444 -82.811236 41.719589000580818 -82.810518001459911 41.720625999999996 -82.810518 41.721547 -82.811039999999991 41.722273 -82.811616 41.723093 -82.812624 41.723257 -82.813013 41.72368 -82.813966999999991 41.72416 -82.816347999999991 41.724427999999996 -82.81888 41.724503 -82.819583999999992 41.724548999999996 -82.820409 41.724433999999995 -82.820866999999993 41.724183 -82.821569 41.723656999999996 -82.822729 41.723451 -82.823706 41.722809999999996 -82.82571999999999 41.722352 -82.8263 41.720065 -82.82746 41.719756 -82.82750999999999 41.718761 -82.82767299999999 41.718075 -82.827947999999992 41.71732 -82.828955 41.716885 -82.830603 41.716634 -82.831 41.716083999999995 -82.832037 41.71565 -82.832709 41.715168999209283 -82.832954000410936 41.714346 -82.832892 41.713637 -82.832403 41.713248 -82.83164 41.713043 -82.831516999999991 41.712699000512259 -82.831517000881448 41.711922 -82.831914 41.711258 -82.832585999999992 41.711234999999995 -82.833165999999991 41.711464 -82.834234 41.711487 -82.834752999999992 41.711304 -82.83515 41.710823 -82.835577 41.710227999483223 -82.835759999441933</posList>
        </LinearRing>
      </exterior>
    </Polygon>
    <Polygon>
      <exterior>
        <LinearRing>
          <posList>41.645933999690683 -82.8439370003459 41.645477 -82.84390599999999 41.644403 -82.841771999999992 41.644289 -82.841315 41.643856 -82.840857 41.643603999999996 -82.840278 41.64349 -82.838814 41.643215999999995 -82.838267 41.642172000740175 -82.836885999277143 41.640907 -82.837259 41.640541 -82.837319999999991 41.639419 -82.837869 41.63804700056199 -82.838111000738351 41.637269000336538 -82.838019000665952 41.636582999999995 -82.838324 41.635805 -82.838842 41.634843 -82.84003 41.634110999623005 -82.840425999186834 41.633241999999996 -82.840243 41.632075999508686 -82.8402420010428 41.631411 -82.840668 41.630610999999995 -82.841064 41.629466999381826 -82.841977999392441 41.629402999999996 -82.841985 41.628322999999995 -82.84209899999999 41.628139999999995 -82.841673 41.628163 -82.84149 41.628803999999995 -82.838472 41.628948 -82.837279 41.629124999999995 -82.83582 41.629194 -82.835209999999989 41.629331 -82.834662 41.630018 -82.833289999999991 41.630092999999995 -82.833169 41.63091 -82.831858 41.631436 -82.831291999999991 41.632099 -82.830578 41.633015 -82.828474 41.633700999999995 -82.827011 41.636148999999996 -82.824998999999991 41.636972 -82.824084 41.637955999999996 -82.82353599999999 41.638573 -82.822469 41.638912 -82.821754 41.639185999999995 -82.821191 41.639742 -82.820054 41.639831 -82.819876999999991 41.640701 -82.81939 41.641227 -82.818840999999992 41.64131 -82.818488 41.641448 -82.817906999999991 41.641501 -82.817681999999991 41.641776 -82.817011999999991 41.642348 -82.816707 41.642942 -82.816188 41.643696999999996 -82.813749 41.643800999999996 -82.81361 41.644086 -82.813231 41.644194 -82.813154 41.644771999999996 -82.812743 41.645846999999996 -82.812499 41.645939 -82.812407999999991 41.646945000808664 -82.812254999658307 41.647044 -82.812255 41.647746 -82.812255 41.648683 -82.81277399999999 41.649530000044223 -82.813048000376909 41.650376 -82.81292599999999 41.651039 -82.812591 41.651109999999996 -82.812513 41.651690999791015 -82.812117000166708 41.651903 -82.812176999999991 41.652083999999995 -82.812237 41.652144 -82.812388 41.652235 -82.812598999999992 41.652536000535513 -82.812599000948822 41.652716999999996 -82.812449 41.652747999999995 -82.811966 41.652843 -82.811838 41.653155999999996 -82.811425 41.654264999999995 -82.80975 41.655001999999996 -82.808263 41.655018999999996 -82.807935 41.655041 -82.80753 41.653749 -82.8053 41.653555 -82.803533 41.653600999999995 -82.803136999999992 41.653852 -82.802741 41.654401 -82.802556 41.654554 -82.802526 41.655179 -82.802404 41.655795999999995 -82.801855 41.656459 -82.800482 41.6571 -82.79972 41.657213999999996 -82.798957 41.657649 -82.798256 41.658197 -82.797493 41.659065999999996 -82.796821999999992 41.659945 -82.796421 41.660475 -82.796179 41.660804 -82.79602899999999 41.661283999999995 -82.795602 41.661296 -82.795583999999991 41.661992999999995 -82.794534 41.662496 -82.794198 41.663959999999996 -82.793802 41.664010999999995 -82.793751 41.664494999999995 -82.793266 41.6646920002521 -82.793069000805616 41.665262999999996 -82.793832 41.665870999999996 -82.795236 41.665949999999995 -82.795418 41.665973 -82.79633299999999 41.665607 -82.797216999999989 41.665461 -82.797754 41.665426 -82.797888 41.665236 -82.799258999999992 41.665248 -82.800202 41.664899 -82.801543 41.664428 -82.802121 41.664049999999996 -82.802821 41.664054 -82.803156 41.664415 -82.80465 41.664414 -82.805056999999991 41.66437 -82.805281 41.663405999999995 -82.806575 41.661682 -82.808587 41.659428 -82.824731 41.659524 -82.824821 41.659729999999996 -82.825521999999992 41.659684 -82.826285 41.659248999999996 -82.827474 41.658448 -82.829090999999991 41.658014 -82.830158 41.657899 -82.830799 41.657486999999996 -82.83336 41.657121 -82.834092 41.656389 -82.835129 41.653209 -82.837353999999991 41.652454 -82.83787199999999 41.65112 -82.839201 41.650281 -82.840036 41.649741 -82.840801 41.649549 -82.841073 41.648908 -82.841468999999989 41.648405 -82.841956 41.647766999999995 -82.84281399999999 41.647583999999995 -82.84319099999999 41.647149 -82.843649 41.646843 -82.843791 41.645933999690683 -82.8439370003459</posList>
        </LinearRing>
      </exterior>
    </Polygon>
    <Polygon>
      <exterior>
        <LinearRing>
          <posList>41.681655000723282 -82.8568309989808 41.681222 -82.8566 41.680854 -82.856403 41.68042 -82.855944999999991 41.680237 -82.855335 41.680169 -82.854388 41.680077999999995 -82.853442 41.679735 -82.852801 41.679232 -82.852313 41.678568999999996 -82.852069 41.677623 -82.851647 41.67754 -82.85161 41.676786 -82.849261 41.676809 -82.848529 41.676901 -82.848224 41.677267 -82.847338999999991 41.677656 -82.845413 41.677748 -82.84496 41.6787540002745 -82.84419800078183 41.678878 -82.844242 41.679097 -82.84432 41.679348999999995 -82.844839 41.679463 -82.845663 41.679623 -82.846913 41.680194 -82.84804299999999 41.680354 -82.848774999999989 41.680582 -82.851094 41.680718999999996 -82.851672999999991 41.681084 -82.853596 41.681861 -82.854817 41.681884 -82.856495 41.681655000723282 -82.8568309989808</posList>
        </LinearRing>
      </exterior>
    </Polygon>
  </surfaceMembers>
</MultiSurface>

尝试对名称使用通配符space,即 *:

<cfdump var="#XmlSearch(parsedXML, '//*:Polygon')#">   

... 或

<cfdump var="#XmlSearch(parsedXML, '//*:MultiSurface/*:surfaceMembers/*:Polygon')#"> 

解释:

我假设您只是 xml 的消费者并且没有能力改变它。这里的问题是父元素包含一个 namespace 声明,即

 <MultiSurface xmlns="http://www.opengis.net/gml">

但是,当前的搜索表达式是为没有的元素设计的,即

 <MultiSurface>

声明的名称space 适用于 MultiSurface 元素及其后代。因此,必须在任何搜索表达式中引用它才能识别这些元素。通常 namespace 声明包含一个 "prefix": xmlns:prefix="http://example.com/widget"。然后在引用元素时将前缀用作一种 shorthand,即 prefix:ElementName。在这种情况下,没有前缀,元素以某种未命名的 space 结尾。所以你的搜索表达式必须通过其他方式引用它,例如

  1. 通配符

    xmlSearch(parsedXML, "//*:MultiSurface")
    
  2. local-name()

    xmlSearch(parsedXML, "/*[local-name()='MultiSurface']")
    
  3. namespace-uri()

    xmlSearch(parsedXML, "/*[local-name()='MultiSurface' 
                and namespace-uri() = 'http://www.opengis.net/gml']")
    

每种方法都有利有弊。方法#1 和#2 适用于简单模式并在 any namespace 中查找匹配元素。对于更复杂的模式,在多个 namespace 中定义相同的元素,需要方法 #3 来解决歧义。

我喜欢使用 XMLParse,因为它可以让我很快重新使用 ColdFusion 结构和数组。我的代码没有 return 数组。您可能需要添加代码来将多边形作为单个项目或数组处理。

 ...
    </LinearRing>
  </exterior>
</Polygon>   </surfaceMembers> </MultiSurface>

</cfsavecontent>


<cfdump var="#isXML(parsedXML)#">    // return YES

<cfset dataXML = XMLParse(parsedxml)>

<cfdump var="#dataXML.MultiSurface.surfaceMembers.Polygon#">

参见:https://cffiddle.org/app/file?filepath=f4a9d905-110c-4a24-9102-e3812f17fe9b/4902bb97-63b6-4b91-b067-8990b8826819/6b3c45ca-ef2b-448a-abb9-4543ed17f392.cfm

您的问题是为什么您的搜索没有返回任何结果。答案是您的 XML 指定了命名空间 (xmlns),而您的搜索没有指定命名空间。

你说你需要帮助来更正代码。所以这里是:

<cfxml variable="parsedXML" >
<n:MultiSurface xmlns:n="http://www.opengis.net/gml">
  <n:surfaceMembers>
    <n:Polygon>
      <n:exterior>
        <n:LinearRing>
          <n:posList>41.710227999483223 etc.</n:posList>
        </n:LinearRing>
      </n:exterior>
    </n:Polygon>
    <n:Polygon>
      <n:exterior>
        <n:LinearRing>
          <n:posList>41.645933999690683 etc.</n:posList>
        </n:LinearRing>
      </n:exterior>
    </n:Polygon>
    <n:Polygon>
      <n:exterior>
        <n:LinearRing>
          <n:posList>41.681655000723282 etc.</n:posList>
        </n:LinearRing>
      </n:exterior>
    </n:Polygon>
  </n:surfaceMembers>
</n:MultiSurface>
</cfxml>
<cfset searchResult1=xmlSearch(parsedXML, "//n:MultiSurface")>
<cfset searchResult2=xmlSearch(parsedXML, "//n:surfaceMembers")>
<cfset selectedElements = XmlSearch(parsedXML,'/n:MultiSurface/n:surfaceMembers/n:Polygon')>

<cfdump var='#searchResult1#' label="n:MultiSurface">
<cfdump var='#searchResult2#' label="n:surfaceMembers">
<cfdump var='#selectedElements#' label="n:Polygon">