2013-03-18 22 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