wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes accessing Attribute values of element. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "accessing Attribute values of element." Watch "accessing Attribute values of element." New topic
Author

accessing Attribute values of element.

Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
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!!
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
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>

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Thanks a lot Mapraputa Is!!
That worked beautifully!!
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
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
 
jQuery in Action, 2nd edition
 
subject: accessing Attribute values of element.