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>
to look exactly like this:
<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>
Please help.
Thanks!
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.
Try it this way:
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>