2014-06-17 109 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你是對的。即使它在技術上是相同的,這使得它更清楚可能 –