XML转换为CSV
问题描述:
我需要使用XSLT文件将某些XML转换为CSV文件。 XML代码以下面的缩写形式列出。 CSV格式我正在寻找的是 主机名,generationDate,设备ID所需的CSVXML转换为CSV
DESKTOP-H3B7ONO,20171014T100948Z,3A80-071084
的
例子,我只需要从XML文件3个值,其余的可以忽略不计。
<?xml version='1.0' encoding='UTF-8' ?>
<document>
<properties>
<basic>
<property id="hostName">
<value>DESKTOP-H3B7ONO</value>
</property>
<property id="generationDate">
<value>20171014T100948Z</value>
</property>
<property id="rotationType">
<value></value>
</property>
<!-- ... many properties omitted here ... -->
<property id="generationEpoch">
<value>1507975788796</value>
</property>
<property id="machineId">
<value>3A80-071084</value>
</property>
<property id="application">
<value>scan</value>
</property>
<!-- ... more properties omitted ... -->
</basic>
</properties>
</document>
输入中只有一条记录(basic
元素不重复)。
我曾尝试以下样式表
<?xml version="1.0" encoding="Utf-8" ?>
<xsl:stylesheet xmlns:xsl="w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text" encoding="Utf-8" />
<xsl:template match="basic">
<xsl:for-each select="property">
<xsl:value-of select="value" />
<xsl:text>,</xsl:text>
<xsl:text></xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
答
你靠近。
而是在你的样式表xsl:for-each
的,你可以尝试
<xsl:value-of select="property[@id='hostName']/value"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="property[@id='generationDate']/value"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="property[@id='machineId']/value"/>
那么你需要这三个值中的一个行还是你认为会有几个这三个要素的?在后一种情况下,他们是兄弟姐妹还是对于你感兴趣的三个元素的任何“记录/行”有一个新的“基本”父母?你有什么尝试?任何XPath教程都涵盖了使用XPath选择元素。 –
只有一个记录,所有在基本的,我只对这3个元素感兴趣,我试过以下 的-的值 的xsl:for-each>的 :template> xsl:stylesheet> –
user3129787
关闭选民的注意事项:我无法想出任何方式来理解此问题,并且不能将其理解为aq在帮助中心的范围说明中对编程有疑问。你能解释一下吗?这个问题描述了程序员通常使用的涉及工具的具体编程问题(这里:XSLT作为编程语言),而且问题很实际并且可以回答。 –