xslt中的命名空间问题

时间:2014-06-17 06:45:10

标签: xslt

我正在尝试将以下xml转换为其他xml,但我没有获得xCoordinate和yCoordinate的值。我想将结构从源 - XML转换为Target-XML,其中goocodes将匹配,结果将分配给x和y。

来源 - XML

<?xml version="1.0"?>
<AddressResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" errorCode="0" errorDescription="">
  <wrappedResultList xmlns="http://xlocate.xserver.ptvag.com">
    <ResultAddress city="Amsterdam" city2="" country="NL" houseNumber="" postCode="1***" state="Noord-Holland" street="" adminRegion="Amsterdam" appendix="" classificationDescription="EXACT" countryCapital="Amsterdam" detailLevelDescription="CITY" totalScore="100">
      <wrappedAdditionalFields />
      <coordinates>
        <kml xsi:nil="true" xmlns="http://common.xserver.ptvag.com" />
        <point x="4.89327999999999" y="52.373090000000005" xmlns="http://common.xserver.ptvag.com" />
      </coordinates>
    </ResultAddress>
    <ResultAddress city="Amsterdam-Zuidoost" city2="" country="NL" houseNumber="" postCode="110*" state="Noord-Holland" street="" adminRegion="Amsterdam" appendix="" classificationDescription="EXACT" countryCapital="Amsterdam" detailLevelDescription="CITY" totalScore="80">
      <wrappedAdditionalFields />
      <coordinates>
        <kml xsi:nil="true" xmlns="http://common.xserver.ptvag.com" />
        <point x="4.9513699999999838" y="52.316199999999988" xmlns="http://common.xserver.ptvag.com" />
      </coordinates>
    </ResultAddress>
    <ResultAddress city="Nieuw-Amsterdam" city2="" country="NL" houseNumber="" postCode="7833" state="Drenthe" street="" adminRegion="Emmen" appendix="" classificationDescription="EXACT" countryCapital="Amsterdam" detailLevelDescription="CITY" totalScore="80">
      <wrappedAdditionalFields />
      <coordinates>
        <kml xsi:nil="true" xmlns="http://common.xserver.ptvag.com" />
        <point x="6.8528699999999994" y="52.716139999999982" xmlns="http://common.xserver.ptvag.com" />
      </coordinates>
    </ResultAddress>
  </wrappedResultList>
</AddressResponse>

目标 - XML

<GeoCodeResponse>
<geocordinate>
<xCordinate>4.89327999999999</xCordinate>
<yCordinate>52.716139999999982</yCordinate>
</geocordinate>
</GeoCodeResponse>

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xl="http://xlocate.xserver.ptvag.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://common.xserver.ptvag.com" exclude-result-prefixes="xl xsi xsd cm" version="1.0">
    <xsl:output method="xml" indent="yes"/>
<xsl:template match="/">

    <GeoCodeResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <geocordinate xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<xsl:for-each select="AddressResponse/xl:wrappedResultList/xl:ResultAddress">
 <xsl:sort select="@xl:totalScore" order="descending" data-type="number"/>

<xsl:if test="position()= 1">
<xCordinate> <xsl:value-of select="/xl:coordinates/cm:point/cm:x" /></xCordinate>

<yCordinate>  <xsl:value-of select="/xl:coordinates/cm:point/cm:y" /></yCordinate>
</xsl:if>
 </xsl:for-each>
  </geocordinate>    
    </GeoCodeResponse>
    </xsl:template>  
</xsl:stylesheet>

请帮助在上面的xslt中做些什么。

1 个答案:

答案 0 :(得分:2)

你快到了。坐标是属性,而不是节点。

将其改为:

<xsl:if test="position()= 1">
    <xCordinate>
        <xsl:value-of select="xl:coordinates/cm:point/@x" />
    </xCordinate>
    <yCordinate>
        <xsl:value-of select="xl:coordinates/cm:point/@y" />
    </yCordinate>
</xsl:if>

您也是从根引用坐标节点,但它应该是相对的。我将/xl:coordinaties更改为xl:coordinates