Я хотел бы преобразовать XML в CSV с помощью XSLT. Может, кто-то поможет мне перестроить XSL-файл, чтобы каждая информация, если она существует, правильно хранилась в столбцах в соответствии с заголовок. В противном случае столбец должен оставаться пустым.Преобразование XML в CSV с помощью XSLT
Мои входы
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>[email protected]</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>[email protected]</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>[email protected]</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>[email protected]</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 </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>
У меня есть все разыскиваемых данные в файле вывода за исключением того, что они не хранятся в соответствующих столбцах, и это моя проблема. Ваша помощь приветствуется. :)
Заранее спасибо.
Уважаемый Томалак, используя таблицу стилей XSL, я столкнулся с проблемой, и я хотел бы попросить вас о помощи. Я хотел бы установить условие, чтобы значения связи были выбраны, только если бизнес-партнер «Ролевой тип» - AG или WE. Если столбцы должны оставаться пустыми. – LimeTech18
Прошу прощения, я не буду отвечать на подобные наблюдения. У вас есть решение, которое работает, вам нужно будет потратить время и научиться достаточно об основах XSLT и XPath, которые сделают это изменение самостоятельно. Это не сложно. – Tomalak