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.
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
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.
Joined: Mar 15, 2002
Thanks for the explanation.This is what I was looking for. Vasudha