根据分隔符将特定元素属性拆分为多行 [英] Split specific element attribute into multiple rows based on delimiter

查看:43
本文介绍了根据分隔符将特定元素属性拆分为多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to transform an XML and split the third App_Data element Value into multiple duplicate rows based on the commas from:

    <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

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

Actually, your example shows that you do need an element for each token.

试试这个:

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>

<小时>

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


Or, if you prefer a shorter (but non-reusable) version:

<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>

这篇关于根据分隔符将特定元素属性拆分为多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆