aspose file tools*
The moose likes XML and Related Technologies and the fly likes Handling IDREF in XSL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Handling IDREF in XSL" Watch "Handling IDREF in XSL" New topic
Author

Handling IDREF in XSL

Kamakshi Mahadevan
Greenhorn

Joined: Nov 08, 2000
Posts: 18
Is there any way of handling IDREF attributes using XSL??
I have a requirement where I have to access attributes of the element referred to by the IDREF attribute. For example, my XML document looks like this:
<category id="main" name="Main Category">
<sub id="sub1"/>
<sub id="sub2"/>
<sub id="sub3"/>
</category>
<category id="sub1" name="Sub Category1">
...
...
...
</category>
<category id="sub2" name="Sub Category2">
...
...
...
</category>
<category id="sub3" name="Sub Category3">
...
...
...
</category>
When I encounter the <sub/> element, I want to get the value of name attribute for the corresponding id. Any idea how it is done in XSL??
Kamakshi
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Kamakshi, you can use id() function. In your case it will be something like this:
<xsl:template match="sub">
...
<!�category name -->
<xsl:value-of select="id(@id)/@name"/>
...
</xsl:template>
But this function works only if your id attribute is defined with ID type in DTD, and DTD is available during XSL transformation.

[This message has been edited by Mapraputa Is (edited April 17, 2001).]


Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
Kamakshi Mahadevan
Greenhorn

Joined: Nov 08, 2000
Posts: 18
Mapraputa,
I tried using the id() function. But it does not work. I have defined the id attribute using the ID type in DTD & the DTD is available during transformation.. but it still doesn't work.
Thanks
Kamakshi
[This message has been edited by Kamakshi Mahadevan (edited April 17, 2001).]
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Kamakshi, what XSLT processor do you use to convert XML to HTML? If you use IE5, I read somewhere that it doesn't support id() function. I used Xalan to convert this XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE root SYSTEM "category.dtd">
<root>
<category id="main" name="Main Category">
<sub id="sub1"/>
<sub id="sub2"/>
<sub id="sub3"/>
</category>
<category id="sub1" name="Sub Category1"/>
<category id="sub2" name="Sub Category2"/>
<category id="sub3" name="Sub Category3"/>
</root>
with XSL stylesheet:
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="sub">
<!-- category name -->
<xsl:value-of select="id(@id)/@name"/><br/>
</xsl:template>
</xsl:stylesheet>
and the output in HTML was:
Sub Category1
Sub Category2
Sub Category3
Kamakshi Mahadevan
Greenhorn

Joined: Nov 08, 2000
Posts: 18
Thanks a lot, Mapraputa. It does work!
I am using the Xalan processor too.
I had made a mistake in defining the name & id attributes in the DTD.
Thanks again
Kamakshi
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Another solution: to use key function.
This solution may work even better because you do not have to define id attribute as having ID type. (actually you can get rid of DTD entirely, wheter it is a good or bad idea). The problem with ID type is that its values must be unique within values of ALL attributes of ID type in a document. Suppose that if you have two attributes of ID type, whose values are received from a database. Each attribute has unique id, but there is no guarantee that they do not use the same (unique for them ) id values. Your resulting document will not be valid.
However, key function will work just fine in this case. I added <anotherCategory> element with the same id values to illustrate the point. Also id attribute on sub element was renamed to IDRef, because it has different semantics.
XML:

XSL

P.S. Just reading about it right now
Kamakshi Mahadevan
Greenhorn

Joined: Nov 08, 2000
Posts: 18
yea.. what you say is true.. particularly in my case where my xml document contains a bunch of restaurant menus.. though i do not use a database(atleast at this stage),it is kind of cumbersome to keep generating unique ids for every element(category/item) of each menu that gets added. But not using the ID type leaves us with no way of ensuring that the id values of each category within the same menu are unique,right? So is it only up to the programmer/user to take care of this during XML generation??
also, the mistake I had made in the previous case was defining the id attribute after the name attribute in the DTD & XML( i don't know why anybody would do that, though). The XML specification says:
-----------------------------------------------------------------
the order of attribute specifications in a start-tag or empty-element tag is not significant.
-----------------------------------------------------------------
However, the id() function works only when id comes before name. But the key()function works just fine in both the cases!
Thanks
Kamakshi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling IDREF in XSL