Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Concatenating Nodes into CSV format

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to generate csv file after concatenating some of the fields in XML

My XML:

<TopElement>
<subelement>
<data id="id1">1</data>
<data id="id2">1</data>
<data id="id3">3</data>
<data id="id4">4</data>
</subelement>
</TopElement>

I want to concatenate id1, id2, id4 like 1,1,4 (csv) format.

Can you please help me by with XSL?

Appreciate your help

Thanks
Raj
 
Marshal
Posts: 27286
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What part aren't you clear about? To get the value of a node you use the <xsl:value-of> element. To output a comma, you simply put a comma in your code. The only non-trivial part of the code is controlling white-space, so that you don't get extra blanks or line-endings where you don't want them. You might have to use the <xsl:text> element for that.

Perhaps you could post the code you have so far and tell us what is the problem with it?
 
Raj Chukka
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for the reply. I am new to XSLT.

<xsl:template match="/">
<xsl:for-each select="TopElement/SubElement">
<xsl:if test="data[@name='id1']" >
<xsl:value-of select ="data" />
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

I am not able to print the output for "id1" which is "1" the XSL? Can you please correct me where I am doing wrong?

Thanks,
Raj
 
Paul Clapham
Marshal
Posts: 27286
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This doesn't select anything because the element names in the document ("subElement") don't match the name in the code ("SubElement").

But let's suppose you fix that typo.



At this point your context node is a subElement node. So this statement says "Is there a child of this node which is a data element with a name attribute whose value is 'id1'? The answer is yes, there is.



This says "Find all of the data elements which are children of this node and concatenate all of the text nodes which are children of them. There are four such data elements so the result of that is '1234'.



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic