Reputation: 333
I have variable 'temperatureQualifier' whose type is array. I need to read that array variable and extract each value from the array and use it in my XSLT.
Sample Input XML is
<document>
<item>
<gtin>1000909090</gtin>
<flex>
<attrGroupMany name="tradeItemTemperatureInformation">
<row>
<attr name="temperatureQualifier">[10, 20, 30, 40]</attr>
</row>
</attrGroupMany>
</flex>
</item>
</document>
Desired Output XML should be
<?xml version="1.0" encoding="UTF-8"?>
<CatalogItem>
<RelationshipData>
<Relationship>
<RelationType>Item_Master_TRADEITEM_TEMPERATURE_MVL</RelationType>
<RelatedItems>
<Attribute name="code">
<Value>10</Value>
</Attribute>
<Attribute name="code">
<Value>20</Value>
</Attribute>
<Attribute name="code">
<Value>30</Value>
</Attribute>
<Attribute name="code">
<Value>40</Value>
</Attribute>
</RelatedItems>
</Relationship>
</RelationshipData>
</CatalogItem>
I am using the below XSLT but it is giving me all values in 1 node only.
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="document">
<CatalogItem>
<RelationshipData>
<Relationship>
<RelationType>Item_Master_TRADEITEM_TEMPERATURE_MVL</RelationType>
<RelatedItems>
<xsl:for-each select="item/flex/attrGroupMany[@name ='tradeItemTemperatureInformation']/row">
<Attribute name="code">
<Value>
<xsl:value-of select="attr[@name='temperatureQualifier']"/>
</Value>
</Attribute>
</xsl:for-each>
</RelatedItems>
</Relationship>
</RelationshipData>
</CatalogItem>
</xsl:template>
</xsl:stylesheet>
Note: Number of value in array can be 1 or more than 1. Example for single value array is [10]
Example for multie value array is [10, 20, 30, 40]
Upvotes: 0
Views: 4705
Reputation: 167696
Using the latest version of Saxon you could try XSLT 3.0 and
<xsl:for-each
select="item/flex/attrGroupMany[@name = 'tradeItemTemperatureInformation']/row/attr[@name = 'temperatureQualifier']/json-to-xml(.)//*:number">
<Attribute name="code">
<Value>
<xsl:value-of select="."/>
</Value>
</Attribute>
</xsl:for-each>
Upvotes: 0
Reputation: 9627
With XST 1.0 you can use a recursive split:
<xsl:template name="split">
<xsl:param name="str" select="."/>
<xsl:choose>
<xsl:when test="contains($str, ',')">
<Attribute name="code">
<Value>
<xsl:value-of select="normalize-space(substring-before($str, ','))"/>
</Value>
</Attribute>
<xsl:call-template name="split">
<xsl:with-param name="str" select="substring-after($str, ',')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<Attribute name="code">
<Value>
<xsl:value-of select="$str"/>
</Value>
</Attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
And call it:
<xsl:call-template name="split">
<xsl:with-param name="str" select="substring-before(
substring-after(
attr[@name='temperatureQualifier'], '[' )
,']' )"/>
</xsl:call-template>
Upvotes: 1