0

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]

1
  • You've tagged this xslt-2.0 but are you actually able to use an XSLT 2.0 processor? If so, you would be able to make use of the tokenize function. Commented Mar 23, 2016 at 11:10

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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>

1 Comment

Sorry!! I forgot to maintain that it was for v1.0. Anyways this solution can help others.. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.