• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Handling IDREF in XSL

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Kamakshi Mahadevan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic