1

i need to convert XML files into CSV .

Expected Output :

tag1  tag2   Typearray
Name1 Name2     1
Name1 Name2     13

using XSLT am able to get only one record the second record in the array is getting rejected.

tag1,tag2,CommOffer,TypeArray
name1,name2,1

Can you please let me know how to read the array value using XSLT ?

Sample XML :

    <Master2>
       <Child1>
          <tag1>name1</tag1>
          <tag2>name2</tag2>
          <TypeArray>
             <value>1</value>
             <value>13</value>
          </TypeArray>     
      </Child1>
    </Master2>
</Master1>

XSLT :

  <xsl:template match="/"> 
      <xsl:text>tag1,tag2,TypeArray</xsl:text> 
         <xsl:text>&#xA;</xsl:text> 
         <xsl:for-each select="Master1/Master2/Child1">
             <xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="normalize- 
              space(tag1)"/></xsl:call-template>
             <xsl:text>,</xsl:text>
             <xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="normalize- 
              space(tag2)"/></xsl:call-template>
             <xsl:text>,</xsl:text>
    <xsl:call-template name="CsvEscape"><xsl:with-param name="value" select="TypeArray/value"/> 
         <xsl:text>&#xA;</xsl:text>
    </xsl:for-each>
 </xsl:template> 
2
  • You tagged this sas - are you using SAS to write this out? Commented Mar 2, 2021 at 17:42
  • yes am using PROC XSL in sas 9.4M2 Commented Mar 3, 2021 at 7:06

1 Answer 1

2

It is difficult to deduce the required logic from a single example. If - as it seems - you want to create a row for each TypeArray/value, with the tag1 and tag2 values repeated in each row, you could do something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />

<xsl:template match="/Master1">
    <!-- header -->
    <xsl:text>tag1,tag2,TypeArray&#10;</xsl:text>
    <!-- data -->
    <xsl:for-each select="Master2/Child1">
        <xsl:variable name="tags">
            <xsl:value-of select="tag1"/>
            <xsl:text>,</xsl:text>
            <xsl:value-of select="tag2"/>
            <xsl:text>,</xsl:text>
        </xsl:variable>
        <xsl:for-each select="TypeArray/value">
            <xsl:value-of select="$tags"/>
            <xsl:value-of select="."/>
            <xsl:text>&#10;</xsl:text>
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

Comments

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.