tags:

views:

450

answers:

3

Suppose we have a stylesheet which pulls in metadata using the key() function. In other words we have instance documents like this:

<items>

<item type="some_type"/>

<item type="another_type"/>

</items>

and a table of additional data we would like to associate with items during processing:

<item-meta>

<item type="some_type" meta="foo"/>

<item type="another_type" meta="bar"/>

<item type="yet_another_type" meta="baz"/>

</item-meta>

Finally, suppose we want to do schema validation on the instance document, restricting the type attributes to the set of types which occur in item-meta. So in the schema we want to use key/keyref instead of restriction/enumeration. This is because using restriction/enumeration will require making a separate list of valid type attributes.

However, it doesn't look like key/keyref will actually work. Having tried it (with MSXML 6.0) it appears the selector of a schema key won't accept the document() function in its xpath argument, so we can't examine the item-meta data, whether it appears in an external file or in the schema file itself. It looks like the only place we can look for keys is the instance document.

So if we really don't want to have a separate list of valid types, we have to do a pre-validation transform, pulling in the item-meta stuff, then do the validation, then do our original transform. That seems overcomplicated for what ought to be a relatively straightforward use of XML schema and stylesheets.

Is there a better way?

+1  A: 

Selectors in key/keyref allow only a very restricted xpath syntax. Short, but not completely accurate: The selector must point to a subnode of the element declared.

The full definition of the restricted syntax is -> here.

So, no I don't see a better way, sorry.

BTW: The W3C states that this restriction was made to make life easier on implementers of XML Schema processors. Keep in mind that one of the design goals of XML Schema was to make it possible to process a document in streaming mode. That explains really a lot of the sometimes seemingly random restrictions of XML Schema.

TToni
A: 

Having thought about it a little more, I came up with the idea of having the stylesheet do that part of the validation. The schema would define the item type as a plain string, and the stylesheet would emit a message and stop processing if it couldn't look up the item type in the item-meta table.

This solution fixes the original problem of having to write down the list of valid types more than once, but it introduces the problem that validation logic is now mixed in with the stylesheet logic. I don't have enough experience with XSD+XSLT to tell whether this new problem is less serious than the old one, but it seems to be more elegant than what I wrote earlier about pulling the item-meta table into each instance document in a pre-validation transform.

A: 

You wouldn't need to stop the XSLT with some error. Just let it produce something that the schema won't validate and that points to the original problem like

 <error txt="Invalid-Item-Type 'invalid_type'"/>

Apart from that please keep in mind that there are no discussion threads here. The posts may go up and down, so it's better to edit your question accordingly.

Remember, the philosophy here is "One question, and the best answer wins".

TToni