Re: [xsl] what to do if id() doesn't recognize xml:id

Subject: Re: [xsl] what to do if id() doesn't recognize xml:id
From: TW <zupftom@xxxxxxxxxxxxxx>
Date: Fri, 14 Jan 2011 12:18:38 +0100
2011/1/14 Michael Kay <mike@xxxxxxxxxxxx>:
> On 14/01/2011 09:55, TW wrote:
>>
>> I'm currently fathoming what's possible with keys as they are really
>> quite handy.  I'm not sure yet whether I fully understand how they
>> work.  In a short flash of excitement I thought I could do something
>> like the following:  If I have data like this
>>
>> <?xml version="1.0"?>
>> <?xml-stylesheet type='text/xsl' href='xsl/musx2svg.xsl'?>
>> <children>
>>   <mother xml:id="mother1" name="Laura">
>>     <child father="father1" name="Kim"/>
>>     <child father="father2" name="Peter" />
>>   </mother>
>>   <mother xml:id="mother2" name="Sarah">
>>     <child name="Ron" father="father2" />
>>   </mother>
>>   <father xml:id="father1" name="Jim">
>>     <child mother="mother1"/>
>>   </father>
>>   <father name="George"/>
>> </children>
>>
>> where children can either be stored as child elements of their mothers
>> or fathers, I could maybe directly select the mother of the current
>> <child>  element like "key('mother',.)" regardless of how they are
>> stored if I was using a key like
>>
>> <xsl:key name="mother" match="mother" use="child|//child[@mother =
>> current()/@xml:id]"/>
>
> Well, for starters, it's a pretty strange way of representing your data.
>

Strange in what regard?  You mean that children are either placed
"inside" their mothers or fathers while the respective other parent is
referenced via ID?  I've a different "multi-hierarchical" context that
would be too confusing to explain here.  But you're right, maybe it
would be less strange if one would simply flatten the hierarchy here
and make it something like

  <mother xml:id="mother1" name="Laura"/>
  <child xml:id="child1" mother="mother1" father="father1" name="Kim"/>
  <child xml:id="child2" mother="mother1" father="father2" name="Peter"/>
  <mother xml:id="mother2" name="Sarah"/>
  <child xml:id="child3" mother="mother2" father="father2" name="Ron"/>
  <father xml:id="father1" name="Jim"/>
  <child xml:id="child4" mother="mother1" father="father1" father
name="Sandy" />
  <father xml:id="father2" name="George"/>

or use a more RDF like approach.  The thing is, in most cases, a
"standard" hierarchy (like children-always-inside-mothers) will be
good and useful, but there are exceptions that need to be handled.  In
the example context, you might think of motherless children.  Or if
you would want to map both legal and genetic parentship, both
hierarchies would be identical in most cases, but you'll nevertheless
have to handle the exceptions.


> But I think you're making it too complicated. Define a simple key
>
> <xsl:key name="id" match="*" use="xml:id"/>
>
> and then a function
>
> <xsl:function name="mother" as="element(mother)*">
> <xsl:param name="child" as="element(child)"/>
> <xsl:sequence select="$child/parent::mother | key('id', $child/@mother)"/>
> </xsl:function>
>

Yes, this would of course do it, but unfortunately today's browsers
won't like it.

Thanks!

Thomas W.

Current Thread