Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ID/IDREF association in DTD

 
Vasudha Deepak
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation.This is what I was looking for.
Vasudha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic