需要动态重命名 XML 元素,然后填充后续同级元素值的值



我已经将CSV转换为XML。需要动态重命名 XML 元素,然后填充后续同级元素值的值。转换为 XML 后,第一个 CSV 列默认为 XML 元素名称<col1>。第二列<col2>等。

请求 #1:使用元素的值重命名默认元素名称。例如,<col1>Account_Name</col1>需要转换为<Account_Name>

请求#2:我需要填充新重命名的元素(即<Account_Name>) 以及每个后续<row>的元素值。例如,第二个<row>应该是<Account_Name>Acct ABC</Account_Name>

第一个<row>实例包含一个<Record>元素,该元素包含多个日期值。从第二个<row>实例开始,<col4><col5>中的值表示"金额"值。因此,金额与第一个<row>上列出的日期相关。(即 col4 代表日期 1/5/2020...Acct ABC 在 2020 年 1 月 5 日的金额为 5。Acct ABC 在 2020 年 1 月 6 日的金额为 123)。

源 XML:

<?xml version='1.0' encoding='UTF-8'?>
<root>
<row>
<col1>Account_Name</col1>
<col2>Account_Code</col2>
<col3>Level_Name</col3>
<Record>
<col4>1/5/2020</col4>
<col5>1/6/2020</col5>
</Record>
</row>
<row>
<col1>Acct ABC</col1>
<col2>x123</col2>
<col3>Level 1A</col3>
<Record>
<col4>5</col4>
<col5>123</col5>
</Record>
</row>
<row>
<col1>Acct XYZ</col1>
<col2>x456</col2>
<col3>Level 1A</col3>
<Record>
<col4>456</col4>
<col5>0</col5>
</Record>
</row>
</root>

所需的 XSLT 输出:

<?xml version='1.0' encoding='UTF-8'?>
<root>
<row>
<Account_Name>Acct ABC</Account_Name>
<Account_Code>x123</Account_Code>
<Level_Name>Level 1A</Level_Name>
<Record>
<Date>1/5/2020</Date>
<Amount>5</Amount>
</Record>
<Record>
<Date>1/6/2020</Date>
<Amount>123</Amount>
</Record>
</row>
<row>
<Account_Name>Acct XYZ</Account_Name>
<Account_Code>x456</Account_Code>
<Level_Name>Level 1A</Level_Name>
<Record>
<Date>1/5/2020</Date>
<Amount>456</Amount>
</Record>
<Record>
<Date>1/6/2020</Date>
<Amount>0</Amount>
</Record>
</row>
</root>

提前感谢您的时间。我会提供一个示例 XSLT 解决方案,但老实说,我现在没有任何值得分享的东西。

我建议你试试这种方式:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="root">
<xsl:variable name="dates" select="row[1]/Record/*"/>
<root>
<xsl:for-each select="row[position() >1]">
<row>
<Account_Name>
<xsl:value-of select="col1"/>
</Account_Name>
<Account_Code>
<xsl:value-of select="col2"/>
</Account_Code>
<Level_Name>
<xsl:value-of select="col3"/>
</Level_Name>
<xsl:for-each select="Record/*">
<xsl:variable name="i" select="position()"/>
<Record>
<Date>
<xsl:value-of select="$dates[$i]"/>
</Date>
<Amount>
<xsl:value-of select="."/>
</Amount>
</Record>
</xsl:for-each> 
</row>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>

这允许您拥有任意数量的日期列。

请尝试以下 XSLT。它使用所谓的身份转换模式。

它会给你一个良好的开端。

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="col1">
<Account_Name>
<xsl:value-of select="."/>
</Account_Name>
</xsl:template>
<xsl:template match="col2">
<Account_Code>
<xsl:value-of select="."/>
</Account_Code>
</xsl:template>
<xsl:template match="col3">
<Level_Name>
<xsl:value-of select="."/>
</Level_Name>
</xsl:template>
<xsl:template match="col4">
<Date>
<xsl:value-of select="."/>
</Date>
</xsl:template>
<xsl:template match="col5">
<Amount>
<xsl:value-of select="."/>
</Amount>
</xsl:template>
<!--ignore first <row>-->
<xsl:template match="row[1]"/>
</xsl:stylesheet>

最新更新