On Wed, 11 Jul 2001, Daniel Einspanjer wrote:

> I have been struggling with trying to get the XPath id() function to
> work in the application I am writting.  I have a DTD that defines an ID
> attribute and an xslt that performs some custom html translation.
> During runtime, I load the xml document using the
> org.apache.xerces.DOMParser, and the XPathAPI.selectNodeList() method to
> find the node I want.  Unfortunately, I am having to use the XPath
> "//element[@ID='" + eid + "']" instead of "id(" + eid + ")".  There are
> two problems with this.  First, it is slow on large documents, and
> second, I have to have a large if nest to keep trying different elements
> until I find the one that matches.
>
> Has *anyone* done anything with DTD id attributes in Xalan?  Could
> someone spare a moment to give me a pointer or maybe even an example?

I've included two small files in which the id() function is used in an xsl
stylesheet, and it works.

I run:
java -jar ../../xalan-j_2_1_0/bin/xalan.jar -IN test.xml -XSL test1.xsl

It was necessary to declare the attribute that serves as an id as type ID
(see test.xml) If you did that, perhaps this attribute information is
not passed (correctly) to xalan through the DOM tree for some reason.

The id() function should look through the whole document in which the
context node of the xpath expression appears. What do you pass to
selectNodeList as the context node?

Regards,
  Erwin

<?xml version="1.0"?>

<!DOCTYPE test [<!ELEMENT d (#PCDATA)> <!ATTLIST d id ID #REQUIRED>]>

<test>
   <a>
     <b id="i1"/>
     <c><d id="i2">Hello world</d>Bye</c>
     <e/>
     <f id="i3"/>
   </a>
</test>

<?xml version="1.0"?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";

                version="1.0">



<xsl:output method="xml" indent="yes"/>



<xsl:template match="/">

   <xsl:copy-of select="id('i2')"/>

</xsl:template>



</xsl:stylesheet>

Reply via email to