Xslt XML到CSV的转换

Xslt XML到CSV的转换,xslt,Xslt,我有一个场景,需要将输入XML转换为CSV文件。输出应该具有每个属性的值及其各自的XPATH。 例如:如果我的输入是 <School> <Class> <Student name="" class="" rollno="" /> <Teacher name="" qualification="" Employeeno="" /> </Class> </School> 一个例子

我有一个场景,需要将输入XML转换为CSV文件。输出应该具有每个属性的值及其各自的XPATH。 例如:如果我的输入是

<School>
    <Class>
        <Student name="" class="" rollno="" />
        <Teacher name="" qualification="" Employeeno="" />
    </Class>
</School>

一个例子并不总是包含一条规则。假设您希望为每个具有任何属性的元素(无论其在文档中的位置)指定一行,并为元素的每个属性指定一列,请尝试:


编辑: 这是一个改进的版本,已更正为可以正确使用嵌套元素

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="*">
    <xsl:param name="path" />
    <xsl:variable name="newpath" select="concat($path, '/', name())" />
    <xsl:apply-templates select="@*">
        <xsl:with-param name="path" select="$newpath"/>
    </xsl:apply-templates>
    <xsl:if test="@*">
        <xsl:text>&#10;</xsl:text>
    </xsl:if>
    <xsl:apply-templates select="*">
        <xsl:with-param name="path" select="$newpath"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="@*">
    <xsl:param name="path" />
    <xsl:value-of select="substring(concat($path, '/', name()), 2)"/>
    <xsl:if test="position()!=last()">
        <xsl:text>, </xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>


请注意,每行中的列数可能会有所不同-这在CSV文档中通常是不可接受的。

到目前为止您尝试了什么?我可以一直迭代到子节点。但无法附加到实际的Xpath。。代码是:请不要在评论中发布代码-改为编辑您的问题。将尝试此操作。谢谢。+1我认为该用户没有花时间仔细研究您的答案,因为他/她当时急于删除用户帐户。@MathiasMüller,这是因为这是最终答案;所有其他的答案都来自它,没有其他问题是必要的。然后你就用“深思熟虑”划清了界限。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text" encoding="UTF-8"/>

<xsl:template match="*">
    <xsl:param name="path" />
    <xsl:variable name="newpath" select="concat($path, '/', name())" />
    <xsl:apply-templates select="@*">
        <xsl:with-param name="path" select="$newpath"/>
    </xsl:apply-templates>
    <xsl:if test="@*">
        <xsl:text>&#10;</xsl:text>
    </xsl:if>
    <xsl:apply-templates select="*">
        <xsl:with-param name="path" select="$newpath"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="@*">
    <xsl:param name="path" />
    <xsl:value-of select="substring(concat($path, '/', name()), 2)"/>
    <xsl:if test="position()!=last()">
        <xsl:text>, </xsl:text>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>
<Root>
    <Parent parent="1" parent2="1b">
        <Son son="11" son2="11b"/>
        <Daughter daughter="12" daughter2="12b">
            <Grandson grandson="121" grandson2="121b"/>
            <Granddaughter granddaughter="122" granddaughter2="122b"/>
        </Daughter>
        <Sibling/>
    </Parent>
</Root>
Root/Parent/parent, Root/Parent/parent2
Root/Parent/Son/son, Root/Parent/Son/son2
Root/Parent/Daughter/daughter, Root/Parent/Daughter/daughter2
Root/Parent/Daughter/Grandson/grandson, Root/Parent/Daughter/Grandson/grandson2
Root/Parent/Daughter/Granddaughter/granddaughter, Root/Parent/Daughter/Granddaughter/granddaughter2