belwo 是来自 soapUI 的 xml,我面临着将这个 XML 转换为 CSV 并产生预期输出的困难,我尝试了多个 xsl,但没有希望。为这种情况寻找 xsl。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ReadResponse xmlns="http://abcde">
<ReadResult xmlns:a="http://abcde" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Failures xmlns:b="http://abcde"/>
<a:GeneralErrorMessage i:nil="true"/>
<a:Successes xmlns:b="http://abcde">
<b:OpenSuiteStatus i:type="c:QueryStatus" xmlns:c="http://abcde">
<b:Code i:nil="true"/>
<b:ErrorMessage i:nil="true"/>
<b:SourceIndex>0</b:SourceIndex>
<c:Dto xmlns:d="http://abcde">
<d:ColumnNames xmlns:e="http://abcde/Arrays">
<e:string>code</e:string>
<e:string>period_number</e:string>
<e:string>period_start</e:string>
<e:string>period_finish</e:string>
</d:ColumnNames>
<d:ColumnTypes xmlns:e="http://abcde/Arrays">
<e:string>string</e:string>
<e:string>int</e:string>
<e:string>dateTime</e:string>
<e:string>dateTime</e:string>
</d:ColumnTypes>
<d:ParsedSqlQuery>Select * from Table</d:ParsedSqlQuery>
<d:QueryResults xmlns:e="http://abcde/Arrays">
<e:ArrayOfanyType>
<e:anyType i:type="f:string" xmlns:f="http://www.w3.org/2001/XMLSchema">ABC</e:anyType>
<e:anyType i:type="f:int" xmlns:f="http://www.w3.org/2001/XMLSchema">1</e:anyType>
<e:anyType i:type="f:dateTime" xmlns:f="http://www.w3.org/2001/XMLSchema">2012-12-30T00:00:00</e:anyType>
<e:anyType i:type="f:dateTime" xmlns:f="http://www.w3.org/2001/XMLSchema">2013-01-06T00:00:00</e:anyType>
</e:ArrayOfanyType>
<e:ArrayOfanyType>
<e:anyType i:type="f:string" xmlns:f="http://www.w3.org/2001/XMLSchema">DEF</e:anyType>
<e:anyType i:type="f:int" xmlns:f="http://www.w3.org/2001/XMLSchema">2</e:anyType>
<e:anyType i:type="f:dateTime" xmlns:f="http://www.w3.org/2001/XMLSchema">2013-01-06T00:00:00</e:anyType>
<e:anyType i:type="f:dateTime" xmlns:f="http://www.w3.org/2001/XMLSchema">2013-01-13T00:00:00</e:anyType>
</e:ArrayOfanyType>
</e:ArrayOfanyType>
</d:QueryResults>
<d:SqlQuery>Select * from Table</d:SqlQuery>
</c:Dto>
</b:OpenSuiteStatus>
</a:Successes>
<a:Warnings xmlns:b="http://abcde"/>
</ReadResult>
</ReadResponse>
</s:Body>
</s:Envelope>
ABC,1,2013-01-06T00:00:00,2013-01-06T00:00:00
DEF,2,2013-01-06T00:00:00,2013-01-06T00:00:00
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1" />
<xsl:template match="/QueryResults">
<xsl:apply-templates select="ArrayOfanyType" />
</xsl:template>
<xsl:template match="ArrayOfanyType">
<xsl:apply-templates select="*" />
<xsl:if test="not(last())">
<xsl:value-of select="' '" />
</xsl:if>
</xsl:template>
<xsl:template match="ArrayOfanyType/*">
<xsl:value-of select="." />
<xsl:if test="not(last())">
<xsl:value-of select="','" />
</xsl:if>
</xsl:template>
</xsl:stylesheet>
输出:
ABC
1
2017-12-24T00:00:00
2017-12-31T00:00:00
DEF
2
2017-12-31T00:00:00
2018-01-07T00:00:00
Select * from Table
首先,您需要了解命名空间中的节点不能通过没有前缀的本地名称来选择。您必须在样式表中声明相同的命名空间,为其分配一个前缀并在您的select
andmatch
表达式中使用该前缀。
接下来,您的 XML 的根元素是s:Envelope
- 因此/QueryResults
永远不会应用模板匹配。
现在,可以很简单地通过以下方式实现指定的结果:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:e="http://abcde/Arrays">
<xsl:output method="text" encoding="ISO-8859-1" />
<xsl:template match="/">
<xsl:for-each select="//e:ArrayOfanyType">
<xsl:for-each select="e:anyType">
<xsl:value-of select="."/>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
为了获得更好的性能,请//
使用到e:ArrayOfanyType
. 但是为此,您需要为该路径中的元素使用的所有命名空间添加命名空间声明。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句