Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 27th, 2006, 11:03 AM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSD & xPaths

Hello,
I have been working on XML schemas for sometime. I am trying to display all the elements/attributes in an XML Schema document using XSLT. However, this works fine as long as the all the elements are using the regular predefined types. However, when the elements are actually user-defined complex-types and if I try to generate the xPaths, I get only the ones directly includes as elements and not the ones in the user-defined types.
Here is an example.

I have a test.xsd with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:include schemaLocation="include1.xsd"/>
<xs:element name="rootElement">
        <xs:complexType>
    <xs:sequence>
            <xs:element name="Person1" type="PersonType"/>
    </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

and the included file contents are.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="AddressType">
        <xs:sequence>
    <xs:element name="Street" type="xs:string"/>
    <xs:element name="City" type="xs:string"/>
    <xs:choice>
        <xs:element name="Primary" type="xs:string"/>
        <xs:element name="Vacation" type="xs:string"/>
    </xs:choice>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="PersonType">
    <xs:sequence>
        <xs:element name="Name" type="xs:string"/>
        <xs:element name="Addr" type="AddressType"/>
    </xs:sequence>
</xs:complexType>
</xs:schema>

Now when I run a simple XSL to render all the leaf-xPaths in test.xsd, I get only upto PersonType. However, I would like it to recursively go into the PersonType type and include all the elements from it. Here is the sample output I am looking for. It may not be accurate but it should give an idea of what I am looking for.


xs:schema/xs:element
/xs:schema/xs:element/@name == rootElement
/xs:schema/xs:element/xs:complexType/xs:sequence/xs:element
/xs:schema/xs:element/xs:complexType/xs:sequence/xs:element/@name == Person1
/xs:schema/xs:element/xs:complexType/xs:sequence/xs:element
/xs:schema/xs:element/xs:complexType/xs:sequence/xs:element/@type == PersonType
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[1]
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[1]/@name == Name
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[1]
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[1]/@type == xs:string
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[2]
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[2]/@name == Addr
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[2]
/xs:schema/xs:complexType[1]/xs:sequence/xs:element[2]/@type == AddressType
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[1]
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[1]/@name == Street
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[1]
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[1]/@type == xs:string
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[2]
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[2]/@name == City
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[2]
/xs:schema/xs:complexType[2]/xs:sequence/xs:element[2]/@type == xs:string
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[1]
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[1]/@name == Primary
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[1]
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[1]/@type == xs:string
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[2]
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[2]/@name == Vacation
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[2]
/xs:schema/xs:complexType[2]/xs:sequence/xs:choice/xs:element[2]/@type == xs:string

Please let me know how I can achieve it in XSL.

Thanks in advance,

SG

 
Old March 27th, 2006, 11:21 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Attempting to analyze schema documents in XSLT is a fairly challenging undertaking. I would suggest a multi-pass approach, for example the first pass should follow the "include" links to create a single document containing a module and everything it includes and imports. Then you can define keys on this document to retrieve objects such as complex types by name. You will need to handle QNames properly - the simplest way of doing this is to make your stylesheet schema-aware (using the schema-for-schemas S4S as the input schema) and then the QNames will match up automatically.

Really your question is a very open-ended one. If there's a specific problem you're having with code that doesn't work, we can probably help you solve it; but the overall design of what you're doing is too big a task to tackle in a simple response like this.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 27th, 2006, 11:39 AM
Registered User
 
Join Date: Mar 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Michael,
Actually, we have a schema that is pretty huge and we need to see all the fields included in it so we can understand which fields need to included in the resulting XML based on the field list as it is quicker. Of course, from your suggestion and what I have seen so far, it looks like the schema diagram feature in XML Spy might be able to give us the same information with little effort.
But what I am also trying to do is based on the XSD, I need to define an XSL to create an output XML that confirms to the schema. This will be used in our code to transform a generic XML to a schema-specific document. I am trying to see if I can find a way to create an XSL/XML document that can include all the elements from the XSD then I can just use that and substitute the sample data with the actual fields in the XML document. I have tried to generate a sample XML document from Schema using XMLSpy but it it includes only one off a choice of items. I am trying to see if there is any better approach to this.

Thanks again for your help.

Regards,
SG








Similar Threads
Thread Thread Starter Forum Replies Last Post
XSD.EXE Gen’d C# Deserializing w/ .xsd Subst Group greenstone XML 0 November 9th, 2007 09:27 PM
using 3 XPaths to index using xsl:key Chamkaur XSLT 3 March 2nd, 2007 04:09 AM
XML To XML, using XSL & XSD supercop75 XSLT 1 April 8th, 2006 02:48 AM
Merging XSD files in one XSD file by using what? haoxuqian XML 1 November 4th, 2005 01:42 PM
Linux & KDE & C++ & QT & MYSQL & Kdevelop Munnnki Linux 0 January 2nd, 2005 05:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.