2013-03-18 23 views
-1

我是xslt的新手,我很難弄清楚如何轉換下面的xml。我知道我必須遍歷行,但我不知道如何將列名轉換爲元素。任何幫助將不勝感激。如何使用xslt在xml中轉換數據集

這是從web服務

<?xml version="1.0" encoding="utf-8" ?> 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <soap:Body> 
    <xmlns="http://www.someservice.com/webservices/"> 
    <GetResultsResult> 
    <Columns> 
     <WSColumn> 
     <Name>triptype</Name> 
     </WSColumn> 
     <WSColumn> 
     <Name>description</Name> 
     </WSColumn> 
     <WSColumn> 
     <Name>id</Name> 
     </WSColumn> 
    </Columns> 
    <Rows> 
     <WSRow> 
     <Cell> 
      <anyType xsi:type="xsd:string">vacation</anyType> 
      <anyType xsi:type="xsd:string">Trip to Bahamas</anyType> 
      <anyType xsi:type="xsd:int">89</anyType> 
     </Cell> 
     </WSRow> 
     <WSRow> 
     <Cell> 
      <anyType xsi:type="xsd:string">vacation</anyType> 
      <anyType xsi:type="xsd:string">Trip to California</anyType> 
      <anyType xsi:type="xsd:int">75</anyType> 
     </Cell> 
     </WSRow> 
     <WSRow> 
     <Cell> 
      <anyType xsi:type="xsd:string">business</anyType> 
      <anyType xsi:type="xsd:string">Trip to Chicago</anyType> 
      <anyType xsi:type="xsd:int">82</anyType> 
      </Cell> 
     </WSRow> 
     </Rows> 
     <HasErrors>false</HasErrors> 
     <ErrorMessage /> 
     </GetResultsResult> 
     </GetResultsResponse> 
    </soap:Body> 
</soap:Envelope> 

這收到的XML是改造後期望的結果

<Trips> 
    <Trip> 
    <triptype>vacation</triptype> 
    <description>Trip to Bahamas</description> 
    <id>89</id> 
    </Trip> 
    <Trip> 
     <triptype>vacation</triptype> 
     <description>Trip to California</description> 
     <id>75</id> 
    </Trip> 
    <Trip> 
     <triptype>business</triptype> 
     <description>Trip to Bahamas</description> 
     <id>82</id> 
    </Trip> 
</Trips> 

預先感謝您! 馬塞洛

回答

1

這應做到:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:gr="http://www.someservice.com/webservices/" 
       exclude-result-prefixes="gr"> 

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 
    <xsl:key name="kColumn" match="gr:WSColumn/gr:Name" 
      use="count(../preceding-sibling::gr:WSColumn) + 1" /> 

    <xsl:template match="text()" /> 

    <xsl:template match="/"> 
    <Trips> 
     <xsl:apply-templates /> 
    </Trips> 
    </xsl:template> 

    <xsl:template match="gr:WSRow/gr:Cell"> 
    <Trip> 
     <xsl:apply-templates select="*"/> 
    </Trip> 
    </xsl:template> 

    <xsl:template match="gr:Cell/*"> 
    <xsl:element name="{key('kColumn', position())}"> 
     <xsl:value-of select="."/> 
    </xsl:element> 
    </xsl:template> 
</xsl:stylesheet> 

當你的樣品輸入運行(後破GetResultsResponse標籤是固定的),這將產生:

<Trips> 
    <Trip> 
    <triptype>vacation</triptype> 
    <description>Trip to Bahamas</description> 
    <id>89</id> 
    </Trip> 
    <Trip> 
    <triptype>vacation</triptype> 
    <description>Trip to California</description> 
    <id>75</id> 
    </Trip> 
    <Trip> 
    <triptype>business</triptype> 
    <description>Trip to Chicago</description> 
    <id>82</id> 
    </Trip> 
</Trips> 
+0

JLRishe,謝謝你這麼多答案!!!這正是我正在尋找的... – mcampos 2013-03-18 21:02:22