2014-06-17 196 views
0

我想将下面的xml转换为其他xml,但我没有得到xCoordinate和yCoordinate的值。我想将结构从源XML转换为目标XML,其中goocodes将匹配,并将结果分配给x和y。xslt命名空间问题

来源 - 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来完成。

+0

请注意,样式表头和GeoCodeResponse元素中的xsi和xsd存在冲突的名称空间声明(头文件中的错误)。这是一个错误,尽管它可能不是一个致命错误。 –

回答

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

+0

“*我将/ xl:coordinaties改为./xl:coordinates*”您可以删除'。/'部分。 –

+0

非常感谢您的快速回复。 – user3747335

+0

@ michael.hor257k你是对的。即使它在技术上是相同的,这使得它更清楚可能 –