使用 XSLT 将 XML 转换为 CSV



我想使用 XSLT 将 XML 转换为 CSV。请有人帮我重新排列XSL文件,以便每个数据(如果存在)都根据标题正确存储在列中。否则,该列必须保持为空。

我的输入是

XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<message version="2.0" system="avs/3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <header>
        <creation-date>2016-10-06</creation-date>
        <creation-time>10:46:00</creation-time>
    </header>
    <body>
        <subscription>
            <promotion-source>5011158916</promotion-source>
            <publication>00069329</publication>
            <business-partner id="" role-type="AG">
                <company1>AM</company1>
                <company2>Gebug</company2>
                <company3></company3>
                <communication>
                    <communication-type>T</communication-type>
                    <communication-value></communication-value>
                </communication>
                <communication>
                    <communication-type>F</communication-type>
                    <communication-value></communication-value>
                </communication>
                <communication>
                    <communication-type>WW</communication-type>
                    <communication-value>mm@hvhv.de</communication-value>
                </communication>
            </business-partner>
            <business-partner id="Doc" role-type="WE">
                <last-name1>M</last-name1>
                <first-name>A</first-name>
                <street>str.</street>
                <zipcode>33</zipcode>
                <company1>PM</company1>
                <company2>Geung</company2>
                <company3></company3>
                <communication>
                    <communication-type>T</communication-type>
                    <communication-value>0883</communication-value>
                </communication>
                <communication>
                    <communication-type>F</communication-type>
                    <communication-value>608</communication-value>
                </communication>
                <communication>
                    <communication-type>WW</communication-type>
                    <communication-value>BIN@online.de</communication-value>
                </communication>
                <attribute>
                    <attribute-type>EMNL</attribute-type>
                    <attribute-value>N</attribute-value>
                </attribute>
            </business-partner>
        </subscription>
        <subscription>
            <promotion-source>916</promotion-source>
            <publication>329</publication>
            <business-partner id="Mas" role-type="AG">
                <company1>iß</company1>
                <company2>mp; Co.</company2>
                <company3></company3>
            </business-partner>
            <business-partner id="" role-type="WE">
                <last-name1>ar</last-name1>
                <first-name>ro</first-name>
                <street>str</street>
                <zipcode>858</zipcode>
                <company1>mp;iß</company1>
                <company2>mp;G</company2>
                <company3></company3>
                <communication>
                    <communication-type>T</communication-type>
                    <communication-value></communication-value>
                </communication>
                <communication>
                    <communication-type>F</communication-type>
                    <communication-value></communication-value>
                </communication>
                <communication>
                    <communication-type>WW</communication-type>
                    <communication-value>p@m.de</communication-value>
                </communication>
                <attribute>
                    <attribute-type>L</attribute-type>
                    <attribute-value>N</attribute-value>
                </attribute>
            </business-partner>
        </subscription>
        <subscription>
            <promotion-source>916</promotion-source>
            <publication>009</publication>
            <business-partner id="Mas" role-type="AG">
                <company1>ten</company1>
                <company2>GR</company2>
                <company3></company3>
                <communication>
                    <communication-type>T</communication-type>
                    <communication-value>061</communication-value>
                </communication>
                <communication>
                    <communication-type>F</communication-type>
                    <communication-value></communication-value>
                </communication>
                <communication>
                    <communication-type>WW</communication-type>
                    <communication-value>alff@a-h-architekten.de</communication-value>
                </communication>
            </business-partner>
            <business-partner id="Doc" role-type="WE">
                <last-name1>zek</last-name1>
                <first-name>Ht</first-name>
                <street>Pt</street>
                <zipcode>62</zipcode>
                <company1>ten</company1>
                <company2>GR</company2>
                <company3></company3>
                <attribute>
                    <attribute-type>L</attribute-type>
                    <attribute-value>N</attribute-value>
                </attribute>
            </business-partner>
        </subscription>
    </body>
</message>

这是 xsl 样式表文件:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />
<xsl:template match="/">
    <!-- Output the CSV header -->
    <xsl:text>promotion-source;publication;BP-id;BP-role-type;AG-last-name;AG-first-name;AG-company1;AG-company2;AG-company3;AG-tel;AG-fax;AG-email;BP-id;BP-role-type;WE-last-name;WE-first-name;WE-street;WE-zipcode;WE-company1;WE-company2;WE-company3;WE-tel;WE-fax;WE-email;attribute-type;attribute-value&#10;</xsl:text>
    <!-- Output the value -->
    <xsl:for-each select="message/body/subscription">
        <!-- begin value -->
        <xsl:value-of select="concat(promotion-source, ';', publication,';')" />
        <!-- business-partner values -->
        <xsl:for-each select="business-partner">
            <xsl:value-of select="@id" />
            <xsl:text>;</xsl:text>
            <xsl:value-of select="@role-type" />
            <xsl:text>;</xsl:text>
            <xsl:value-of
                select="concat(last-name1, ';', first-name, ';', street, ';', zipcode, ';', company1, ';', company2, ';', company3, ';')" />
            <xsl:text>;</xsl:text>
            <xsl:for-each select="communication">
                <xsl:value-of select="communication-value" />
            </xsl:for-each>
            <xsl:for-each select="attribute">
                <xsl:value-of select="attribute-type" />
                <xsl:text>;</xsl:text>
                <xsl:value-of select="attribute-value" />
            </xsl:for-each>
        </xsl:for-each>
        <xsl:text>
</xsl:text>
    </xsl:for-each>
</xsl:template>

我在输出文件中拥有所有想要的数据,除了它们没有存储在各自的列中,这是我的问题。感谢您的帮助。:)

提前谢谢你。

业务合作伙伴 AG业务合作伙伴 WE 的列数不同,并且您使用相同的for each来连接这些列,这就是您的列不匹配的原因

您需要一个平面列表作为输出,因此不能使用嵌套结构(for-each 中的 for-each)来创建它。

您似乎想要列出业务合作伙伴及其详细信息,因此您的单个循环应专注于<business-partner>元素,并且应相对于它们选取所有详细信息。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" encoding="UTF-8" />
  <xsl:template match="/">
    <!-- Output the CSV header -->
    <xsl:text>promotion-source;publication;BP-id;BP-role-type;AG-last-name;AG-first-name;AG-company1;AG-company2;AG-company3;AG-tel;AG-fax;AG-email;BP-id;BP-role-type;WE-last-name;WE-first-name;WE-street;WE-zipcode;WE-company1;WE-company2;WE-company3;WE-tel;WE-fax;WE-email;attribute-type;attribute-value&#10;</xsl:text>
    <!-- Output the values -->
    <xsl:for-each select="//business-partner">
      <xsl:value-of select="concat(../promotion-source, ';')">
      <xsl:value-of select="concat(../publication, ';')">
      <xsl:value-of select="concat(@id, ';')">
      <xsl:value-of select="concat(@role-type, ';')" />
      <!-- and so on -->
      <xsl:value-of select="concat(communication[communication-type = 'T']/communication-value, ';')" />
      <!-- and so on -->
      <xsl:value-of select="concat(attribute[attribute-type = 'EMNL']/attribute-value, ';')" />
      <!-- and so on -->
      <xsl:value-of select="'&#xA;' />
    </xsl:for-each>
  </xsl:for-each>
</xsl:template>

每行 XSLT 输出单个值,以保持内容的可读性和整洁性。

相关内容

  • 没有找到相关文章

最新更新