我是xslt的新手,我很难弄清楚如何转换下面的xml。我知道我必须遍历行,但我不知道如何将列名转换为元素。任何帮助将不胜感激。
这是从webservice收到的xml
<?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>
这是转型后的理想结果
<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>
提前谢谢!!! 马塞洛
答案 0 :(得分: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>