[DDI-users] XSLT: How to iterate over contained var's or varGrp's
Mark R. Diggory
mdiggory at latte.harvard.edu
Fri Oct 15 14:05:54 EDT 2004
IF you can guarantee that the DDI's have a DTD or schema declaration in
them, then I would recommend initially sticking with the ID/IDREF for
XSLT 1.0. I've seen considerable discussion about having the keys
function work to tokenize the list of IDREFs as a processing option for
XSLT 2.0. But I think its far off.
Unfortunately ID/IDREF's become useless when the validation grammar is
absent (ie when using a non-validating parser). Its not a bad idea have
a robust solution that can work when the DTD/schema grammar/validation
is absent from the parsed xml content.
As for using exslt functions, they can be very cross platform when it
comes to xslt engines in java or any other language. They are a good
choice in this regard. In many cases theres pure xslt versions of the
functions like tokenize. For instance:
http://www.exslt.org/str/functions/tokenize/str.tokenize.template.xsl
<rant>
There's nothing worse than having a "Content Provider" generate DDI that
doesn't include DTD/schema declarations. How do they guarantee this is
actually a true DDI without them? The grammar and the associated
namespace are truly the only thing that can guarantee that the document
being parsed/transformed is truly a DDI document. At worst, I've seen
developers write DDI content using standard text processing in perl
without any use of an XML parser or grammar.
In most cases I fear, developers aren't even testing for such validation
conditions when producing DDI content. This is as bad (if not worse) as
the historical problems of poorly documented SPSS control cards and
datasets with bad/absent metadata etc. If someones going to be producing
DDI XML content, do the community a favor (and save your reputation in
the process) include a DTD or Schema declaration. Your users will thank you.
</rant>
Andrew, better to make your code more robust to deal with such
situations. Ultimately, the communities going to demand this be the case.
-Mark
Joachim Wackerow wrote:
> Andrew
>
> With a small time lag I read by chance your question.
>
> If an attribute is defined as IDREFS in the DTD like the attribute var
> of the element varGrp and you are using the DDI DTD in the DDI document,
> you could use the XPath function id() (see
> http://www.w3c.org/TR/xpath#section-Node-Set-Functions).
>
> It will give you a node-set, over which you could iterate, like:
>
> <xsl:for-each select="id(/codeBook/dataDscr/varGrp/@var)">
> <xsl:value-of select="labl"/>
> </xsl:for-each>
>
> Achim
>
> Andrew L Arnott wrote:
>
>> Quick question on XSLT:
>>
>> If I have a varGrp with 5 var's referenced in it,
>> <varGrp ID="VG1" var="V1 V2 V3 V4 V5"/>
>>
>> And of course the necessary variables also in the XML, then how do I
>> iterate
>> over those variables in XSLT? I have used EXSLT to tokenize the
>> string and
>> then do a search like this:
>> <xsl:variable name="IDlist"
>> select="ExsltStrings:tokenize(/codeBook/dataDscr/varGrp[@ID='VG1']"/>
>> <xsl:variable name="varList"
>> select="/codeBook/dataDscr/var[@ID=$IDlist]"/>
>> <xsl:for-each select="varList">
>> ...
>> </xsl:for-each>
>>
>> But I want to get away from using extension methods. It seems like
>> something this common would be provided for in XSLT, maybe using
>> xsl:key or
>> something like that. But I just don't know it. Can someone please
>> advise?
>>
>> Thanks,
>> Andrew
>>
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> DDI-users mailing list
>> DDI-users at icpsr.umich.edu
>> http://www.icpsr.umich.edu/mailman/listinfo/ddi-users
>
>
> _______________________________________________
> DDI-users mailing list
> DDI-users at icpsr.umich.edu
> http://www.icpsr.umich.edu/mailman/listinfo/ddi-users
--
Mark Diggory
Software Developer
Harvard MIT Data Center
http://www.hmdc.harvard.edu
More information about the DDI-users
mailing list