我想使用XSLT将XML转换为CSV,但是当对我的输入应用来自标题为XML To CSV XSLT的SO线程的XSL时:
<WhoisRecord>
<DomainName>127.0.0.1</DomainName>
<RegistryData>
<AbuseContact>
<Email> [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) </Email>
<Name>Internet Corporation for Assigned Names and Number</Name>
<Phone>+1-310-301-5820</Phone>
</AbuseContact>
<AdministrativeContact i:nil="true"/>
<BillingContact i:nil="true"/>
<CreatedDate/>
<RawText>...</RawText>
<Registrant>
<Address>4676 Admiralty Way, Suite 330</Address>
<City>Marina del Rey</City>
<Country>US</Country>
<Name>Internet Assigned Numbers Authority</Name>
<PostalCode>90292-6695</PostalCode>
<StateProv>CA</StateProv>
</Registrant>
<TechnicalContact>
<Email> [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection) </Email>
<Name>Internet Corporation for Assigned Names and Number</Name>
<Phone>+1-310-301-5820</Phone>
</TechnicalContact>
<UpdatedDate>2010-04-14</UpdatedDate>
<ZoneContact i:nil="true"/>
</RegistryData>
</WhoisRecord>
我最后得到:
[email protected] Corporation for Assigned Names and Number+1-310-301-5820,
,
,
,
...,
4676 Admiralty Way, Suite 330Marina del ReyUSInternet Assigned Numbers Authority90292-6695CA,
[email protected] Corporation for Assigned Names and Number+1-310-301-5820,
2010-04-14,
我的问题是,结果转换缺少节点(如包含IP地址的DomainName元素),并且一些子节点没有逗号连接(如AbuseContact的子节点)。
我希望看到CSV格式的所有XML输出,字符串如下:“email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)名称与编号分配机构+1-310-301-5820”,以逗号分隔。
我的XSL是相当生 rust 。感谢你的帮助。:)
下面是我使用的XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>
<xsl:strip-space elements="*" />
<xsl:template match="/*/child::*">
<xsl:for-each select="child::*">
<xsl:if test="position() != last()"><xsl:value-of select="normalize-space(.)"/>, </xsl:if>
<xsl:if test="position() = last()"><xsl:value-of select="normalize-space(.)"/><xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
2条答案
按热度按时间ifmq2ha21#
这个简单的转换产生了想要的结果:
注意使用:
以丢弃任何仅含空白的文本节点。
更新:AJ提出的问题是,结果应该按每行的记录/元组进行分组。问题中没有定义记录/元组应该是什么。因此,当前的解决方案解决了纯空白文本节点和缺少逗号的两个问题,但并不旨在将输出摸索成记录/元组。
jtoj6r0c2#
我相信你需要递归的解决方案来解决这个问题。因此,您需要不断潜入树中,直到到达text()节点。如果这个text()节点实际上是最后一个节点的子节点,那么它将添加一个新行。否则,它只是将值加上逗号。
如果节点没有text()节点作为其子节点,则开始递归地挖掘该树。
这不是傻瓜证明,但它产生了这个结果,我与撒克逊结束:
希望这对你有帮助。