aspose file tools*
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
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "ID/IDREF association in DTD" Watch "ID/IDREF association in DTD" New topic
Author

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.
Thanks,
Vasudha


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

Joined: Mar 25, 2002
Posts: 662
Vasudha,
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>
<!ATTLIST child3 attr3 IDREF #REQUIRED>
]>
<root>
<child1 attr1="jaya1"/>
<child2 attr2="jaya2"/>
<!-- We refer to unique id value of an element -->
<child3 attr3="jaya1"/>
</root>
================================================
The following is how we can get to the related elements by using stylesheets -
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="child3">
<xsl:value-of select="name(id(@attr3))"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
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) -
<!ATTLIST child3 attr3 IDREFS #REQUIRED>
AND
<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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="child3">
<xsl:apply-templates select="@*"/>
</xsl:template>
<xsl:template match="@*">
<xsl:for-each select="id(.)">
<xsl:value-of select="name()"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
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.
Thanks.
Vasudha Deepak
Ranch Hand

Joined: Mar 15, 2002
Posts: 86
Thanks for the explanation.This is what I was looking for.
Vasudha
 
jQuery in Action, 2nd edition
 
subject: ID/IDREF association in DTD