I'm trying to take a input element which is a union of types, and produce outputs of the specific type described in the union. The schema for this is as follows:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:test="http://www.example.com/test"
targetNamespace="http://www.example.com/test">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="test:union" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="union">
<xs:complexType>
<xs:attribute name="type" type="xs:string"/>
<xs:attribute name="x" type="xs:string"/>
<xs:attribute name="y" type="xs:string"/>
<xs:attribute name="z" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="a">
<xs:complexType>
<xs:attribute name="x" type="xs:string"/>
<xs:attribute name="y" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:element name="b">
<xs:complexType>
<xs:attribute name="y" type="xs:string"/>
<xs:attribute name="z" type="xs:string"/>
<xs:attribute ref="test:q"/>
</xs:complexType>
</xs:element>
<xs:attribute name="q" type="xs:string"/>
</xs:schema>
What I want is to take an input like the following:
And produce the following:
I started down the path of using schema-attribute(), but I was not able to work with variables since it is
[FONT='Calibri','sans-serif']determined [/font]compile-time. I also understand that it only works with top-level attribute declarations, which I do not have.
I then started working a two stage approach, where the first transformation only copied over the non-empty attributes to the output elements, and then I used a second stage schema-aware transformation to populate the missing (valid but set to 'empty string') attributes via XSD default values. The problems with this approach are that I end up losing all difference between empty-string and unset for optional attributes. I also believe that there would be run-time validation problems in using SA to populate default values if the element were missing required attributes, and this approach wouldn't cover required attributes.
All said, I might pursue the two-stage approach, but would need to greatly restrict the supported use cases. Before going down that path, I was wondering if anyone had ideas on how to this could be made to work. My best, but failed, attempt is as follows:
Any ideas on how to approach this? Can this not be done in straight XSL?