• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

accessing Attribute values of element.

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following XML:
<?xml version="1.0"?>
<catalog>
<cd title="Empire Burlesque" artist="Bob Dylan", country="USA">
<company state="Columbia" price="10.90" year="1985" />
</cd>
<References ref1="1" ref2="2" ref="3" />
</catalog>

I want to write an XSLT, which outputs the attribute values of each element and seperate it by pipe symbol, ie the output should look like:
Empire Burlesque | Bob Dylan | USA
Columbia | 10.90 | 1985
1 | 2 | 3
How do I access the attribute values of each elelment?
Thanks in advance!!
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bala, if you need to output specific attributes or all attributes in specific order,
you can use
<xsl:value-of select="element/@attribute"/>
construction. For example:
<xsl:value-of select="catalog/cd/@title"/>
If you need to output all attributes in whatever order they are, you can write a generic template that will math all attributes:
<xsl:template match="@*">
<xsl:value-of select="."/><xsl:text>|</xsl:text>
</xsl:template>
To call it recursively it a bit tricky. You need to use <xsl:apply-templates> element:

<xsl:template match="*">
<xsl:apply-templates select="@*"/><xsl:apply-templates/>
</xsl:template>
This variant will output
Empire Burlesque|Bob Dylan|USA|Columbia|10.90|1985|1|2|3|
If you really need to get rid of the last "|" symbol, you can direct all the output to a variable, and then use substring() function:
<xsl:template match="/">
<xsl:variable name="attributes">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="substring ($attributes, 1, string-length($attributes)-1)"/>
</xsl:template>
The whole stylesheet will be:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:variable name="attributes">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="substring ($attributes, 1, string-length($attributes)-1)"/>
</xsl:template>
<xsl:template match="*">
<xsl:apply-templates select="@*"/><xsl:apply-templates/>
</xsl:template>
<xsl:template match="@*">
<xsl:value-of select="."/><xsl:text>|</xsl:text>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
 
Bala Krishniah
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Mapraputa Is!!
That worked beautifully!!
 
Bala Krishniah
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have one more question...
I have three different XML files and the corresponding XSLT stylesheets.
WHen I access the XML file from a database(it will be one of the three format), I have to apply a corresponding Stylesheet to it.
Is there a way to identify which XML file I have accesses currently. I am using apache's Xalan for processing.
I was thinking I have to use DOM, read the first elelemt...and based on that associate the correspoding XSLT file to it.
I'm I right?? Or is there another way of doing it.
Thanks
 
Your buns are mine! But you can have this tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!