user3747335
user3747335

Reputation: 13

Namespace issue in xslt

I am trying to convert following xml into other xml but I am not getting the values for xCoordinate and yCoordinate. I would like to convert the structure from source - XML to Target-XML where the goocodes will match and the result would be assigned to x and y.

Source - 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>

Target - 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>

Please help what could be done in above xslt.

Upvotes: 0

Views: 66

Answers (1)

Reinder Wit
Reinder Wit

Reputation: 6615

You were almost there. The coordinates are attributes, not nodes.

change it into this:

<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>

You are also referencing the coordinates node from the root, but it should be relative. I changed /xl:coordinaties into xl:coordinates

Upvotes: 2

Related Questions