How to deal with scoped XML Schema keys in XSLT

D

David Blickstein

I'm dealing with an XML Schema that uses Schema keys. Specifically the
Schema takes advantage of the ability to define the keys to be specific to a
particular scope, which allows duplication between scopes.

That is I can have a schema like this:

<Company Name="Nike">
<Product Name="Sneaker"/>
<Warehouse ProductStored="Sneaker"/>
</Company>
<Company Name="Asics">
<Product Name="Sneaker"/>
<Warehouse ProductStored="Sneaker"/>
</Company>

The key is the "Name" attribute of the "Product" tag and its scope is
limited to the local "Company". And ProductStored is the keyref to that
key. If the key were not scoped, "Sneaker" would be a duplicate but
because it's scoped to the company, each reference isunambiguous.

Now the question I have is... in XSLT, I want to use these scoped keys
because I want to create hyperlinks from the Warehouse output to the
Product.

Is there any support in XSLT to handle locally scoped keys? If not, are
there techniques to deal with this?

Thanks,

Dave Blickstein
 
D

David Carlisle

XSLT2 adds explict support (in a new 3rd argument to key()) to
specifying the scope

in xslt1 you'd normally do something like

<xsl:key name="n" match="Product" use="concat(../@Name,':',@Name)"/>

<xsl:template match="Warehouse">
.....select="key('n',"concat(../@Name,':',@Name)" ...

(If by "support" you meant automatically getting information from the
schema defined key, then xslt doesn't do this, even in the otherwise
schema-aware xslt2 draft.)

David
 
D

David Blickstein

That's exactly the information I was looking for.

I had figured I could do something like what you suggest for XSLT 1.0 but
obviously the new V2 extension to key() is way better.

I wasn't figuring on seeing XSLT-awareness of Schema anytime soon.

Thanks David,

db
 
M

Martin Honnen

David said:
I'm dealing with an XML Schema that uses Schema keys. Specifically the
Schema takes advantage of the ability to define the keys to be specific to a
particular scope, which allows duplication between scopes.

That is I can have a schema like this:

<Company Name="Nike">
<Product Name="Sneaker"/>
<Warehouse ProductStored="Sneaker"/>
</Company>
<Company Name="Asics">
<Product Name="Sneaker"/>
<Warehouse ProductStored="Sneaker"/>
</Company>

The key is the "Name" attribute of the "Product" tag and its scope is
limited to the local "Company". And ProductStored is the keyref to that
key. If the key were not scoped, "Sneaker" would be a duplicate but
because it's scoped to the company, each reference isunambiguous.

Now the question I have is... in XSLT, I want to use these scoped keys
because I want to create hyperlinks from the Warehouse output to the
Product.

XPath 1.0 and XSLT 1.0 do not have any W3C XML schema support, they
predate the XML schema specification by two years I think.
So there is nothing specific to dealing with such keys defined in a schema.
But of course XPath allows you to write expressions relative to a
certain node and that way if you process a <Company> element node you
can certainly look for
Product[@Name = following-sibling::Warehouse/@ProductStored]

And of course XSLT 1.0 has the <xsl:key> element to set up your own keys
for XSLT processing and then use the XSLT key function to lookup stuff.
 
D

David Carlisle

I wasn't figuring on seeing XSLT-awareness of Schema anytime soon.


XSLT2 will (have the option of) seeing all global and local element
declarations, and type declarations from a schema. It just doesn't see
key constraints from a schema. The schema awareness is an optional
feature of xslt2 (which means, today, that you get it if you use the
paid-for version of saxon, and don't get it if you use the free one)

David
 
D

David Blickstein

Is there a freely distributed version of XSLT that supports the XSLT V2
updated version of the key() function (the one with the 3rd argument)?

At the moment, I'm using whatever it is that comes with XMLSpy and Xalan on
HP-UX.
I think XMLSpy supports XSLT V2, I'm not sure about Xalan.

I don't think I have any need for Schema awareness. Only the ability to use
the key function to limit the scope of a key.
db
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top