Re: [xsl] DTD help!

Subject: Re: [xsl] DTD help!
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 10 Apr 2001 21:58:38 +0100
Hi Rajesh,

> keeping in the mind that i have to make sure all my session elements
> have to different, is there any way to get around this?

Well, twisting the question so that it's actually relevant to the
group (i.e. about XSLT), you can test within XSLT whether there is
more than one element with a particular id attribute, even if that
attribute isn't (recognised as) an ID attribute.

You can define a key to index the Session elements by their id
attribute. XSLT doesn't care what kind of value the id attribute has
(it's the validating XML parser that the XSLT processor uses that
will).  So you can make the id attribute a CDATA attribute, as David
said, and then use it with a key:

<xsl:key name="Sessions" match="Session" use="@id" />

Then, if you have a list of the ids (which you can get by going
through all the Session elements in the document and retrieving their
ids), you can see whether the key returns more than one Session with
that id.

You can get all the Session ids with //Session/@id (although there may
well be a more efficient path - you don't say what your source looks

Then you can iterate over them and test whether there's more than one
Session with that id:

  <xsl:for-each select="//Session/@id">
     <xsl:if test="key('Sessions', .)[2]">
        <xsl:message terminate="yes">
           ERROR: There is more than one Session with the same id.

There are also schema languages that will allow you to test for
uniqueness with values that aren't ID attributes, including XML Schema
and Schematron.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread