根据分隔符将特定元素属性拆分为多行

Split specific element attribute into multiple rows based on delimiter

我正在尝试转换一个 XML 并将第三个 App_Data 元素值根据以下逗号拆分为多个重复行:

    <Metadata>
        <App_Data App="VOD" Name="Run_Time" Value="01:30:57"/>
        <App_Data App="VOD" Name="Year" Value="2016"/>
        <App_Data App="VOD" Name="Category" Value="2330, 2470, 1373"/>
    </Metadata>

看起来完全像这样:

    <Metadata>
        <App_Data App="VOD" Name="Run_Time" Value="01:30:57"/>
        <App_Data App="VOD" Name="Year" Value="2016"/>
        <App_Data App="VOD" Name="Category" Value="2330"/>
        <App_Data App="VOD" Name="Category" Value="2470"/>
        <App_Data App="VOD" Name="Category" Value="1373"/>
    </Metadata>

请帮忙。

谢谢!

it only makes the attributes into child -elements when I need attributes

实际上,您的示例表明您确实需要为每个标记一个元素。

这样试试:

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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="App_Data[@Name='Category']">
    <xsl:call-template name="tokenize">
        <xsl:with-param name="text" select="@Value"/>
    </xsl:call-template>
</xsl:template>

<xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="delimiter" select="', '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:if test="$token">
            <App_Data App="{@App}" Name="Category" Value="{$token}"/>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>

或者,如果您更喜欢较短(但不可重复使用)的版本:

<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:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="App_Data[@Name='Category']" name="tokenize">
    <xsl:param name="text" select="@Value"/>
    <xsl:param name="delimiter" select="', '"/>
        <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
        <xsl:if test="$token">
            <App_Data App="{@App}" Name="Category" Value="{$token}"/>
        </xsl:if>
        <xsl:if test="contains($text, $delimiter)">
            <!-- recursive call -->
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
            </xsl:call-template>
        </xsl:if>
</xsl:template>

</xsl:stylesheet>