Extract data from XSD to XSL

I am converting an XML file that should generate some elements based on valid enumeration parameters defined in XSD.

Suppose I have an XSD that declares a type and element something like this:

<xs:simpleType name="optionType" nillable="true"> <xs:restriction base="xs:string"> <xs:maxLength value="50"/> <xs:enumeration value="USERCHOICE"> </xs:enumeration> <xs:enumeration value="DEFAULT"> </xs:enumeration> </xs:restriction> </xs:simpleType> ... <xs:element name="chosenOption" type='optionType'/> ... <xs:element name="availableOption" type='optionType'/> 

The input will contain only the selected parameter, so you can imagine that it looks like this:

 <options> <chosenOption>USERCHOICE</chosenOption> </options> 

I need to have an output that looks like this:

 <options> <chosenOption>USERCHOICE</chosenOption> <!-- This comes from incoming XML --> <!-- This must be a list of ALL possible values for this element, as defined in XSD --> <availableOptions> <availableOption>USERCHOICE</availableOption> <availableOption>DEFAULT</availableOption> </availableOptions> </options> 

Is there a way to get the XSL extract of the USERCHOICE and DEFAULT enumeration values ​​from XSD and create them in the output?

This will work on WebSphere 6 and will be used by the XSLT 1.0 engine. :(

(The schema file does not change often, but it will change now and then, and I would prefer only to update the schema file instead of updating the schema file and XSLT)

+4
source share
1 answer

Here's a prototype assuming your input XML and XSD are as simple as the samples above. Clarified in accordance with the ways in which they may change. If you need help setting up, let me know.

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="1.0"> <xsl:variable name="xsd" select="document('mySchema.xsd')"/> <xsl:template match="/options"> <xsl:copy> <xsl:for-each select="*"> <xsl:variable name="eltName" select="local-name()"/> <xsl:copy-of select="." /> <availableOptions> <xsl:variable name="optionType" select="$xsd//xs:element[@name = $eltName]/@type"/> <xsl:apply-templates select="$xsd//xs:simpleType[@name = $optionType]/ xs:restriction/xs:enumeration"/> </availableOptions> </xsl:for-each> </xsl:copy> </xsl:template> <xsl:template match="xs:enumeration"> <availableOption><xsl:value-of select="@value" /></availableOption> </xsl:template> </xsl:stylesheet> 
+4
source

All Articles