我正在尝试转换 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>
请帮忙。
Thanks!
它只在我需要时将属性放入子元素
属性
实际上,您的示例表明您确实需要每个标记都有一个元素。
试试这样:
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)