This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Product and Other Certifications and the fly likes ID/IDREF association in DTD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "ID/IDREF association in DTD" Watch "ID/IDREF association in DTD" New topic

ID/IDREF association in DTD

Vasudha Deepak
Ranch Hand

Joined: Mar 15, 2002
Posts: 86
I am not clear about how to associate an an IDREF with a particular ID.
If there are 2 elements with ID attribute and there is 3rd element with an IDREF attribute,then how do I specifically associate the IDREF with the ID of the first element and not the second one.

IBM Certified Developer -XML and Related Technologies(141)<br />SCJP2 SCWCD
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
I think this is what you were looking for -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE root[
<!ELEMENT root (child1, child2, child3)>
<!ELEMENT child1 (#PCDATA)>
<!ELEMENT child2 (#PCDATA)>
<!ELEMENT child3 (#PCDATA)>
<!ATTLIST child1 attr1 ID #REQUIRED>
<!ATTLIST child2 attr2 ID #REQUIRED>
<child1 attr1="jaya1"/>
<child2 attr2="jaya2"/>
<!-- We refer to unique id value of an element -->
<child3 attr3="jaya1"/>
The following is how we can get to the related elements by using stylesheets -
<xsl:stylesheet version="1.0"
<xsl:template match="child3">
<xsl:value-of select="name(id(@attr3))"/>
<xsl:template match="text()"/>
id(@attr3) function will return the node which has the id value "ref"erred to by @attr3 and we are printing the name of the node with that id. Please run the stylesheet to see the way in which we can get to the connected element child1.
I have one concern here. Suppose we change the stuff as follows (only changes shown here) -
<child3 attr3="jaya1 jaya2"/>
The following is the way in which we get both the attributes. I got to this after playing around with this stuff for some time
<xsl:stylesheet version="1.0"
<xsl:template match="child3">
<xsl:apply-templates select="@*"/>
<xsl:template match="@*">
<xsl:for-each select="id(.)">
<xsl:value-of select="name()"/>
<xsl:template match="text()"/>
The point here is that id() function can return a node-set as in the above case.

Hope that this helps and please correct me for any mistakes.
Vasudha Deepak
Ranch Hand

Joined: Mar 15, 2002
Posts: 86
Thanks for the explanation.This is what I was looking for.
I agree. Here's the link:
subject: ID/IDREF association in DTD
It's not a secret anymore!