XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Matthew FadoulSubject: How can I make a subset Schema (XSD) using XSL/XQuery in Stylus Studio?
Author: Matthew Fadoul
Date: 01 Oct 2007 05:02 PM
Hello, I'm a new Stylus Studio user, and a bit of an XSL/Xquery newbie, too. Sorry in advance for the simple question...

Anyway, I've got a very large schema file that is killing the JAXB code generator, and I thought that I might use Stylus Studio to shrink down my schema so that I have a more manageable subset schema for code gen, validation, etc.

Note: I could probably edit the XSD file directly, but I wanted a process that was repeatable.

The basic technique I wanted to use was to take any xs:elements whose "name" attibute matched a set of elements that were of interest to me. I would also like to preserve ancestor and descendant nodes.

Using a simple example, let's say I want to preserve "Element-1" and "Element-2" in the following XML schema file:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Element-1">
<xsd:complexType>
<xsd:sequence id="01">
<xsd:element name="ID" type="xsd:integer" default="0"/>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Element-2">
<xsd:complexType>
<xsd:sequence id="02">
<xsd:element name="ID" type="xsd:integer" default="0"/>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Address">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="First Name" type="xsd:string"/>
<xsd:element name="Last Name" type="xsd:string"/>
<xsd:element name="House Number" type="xsd:string"/>
<xsd:element name="Street" type="xsd:string"/>
<xsd:element name="City" type="xsd:string"/>
<xsd:element name="State" type="xsd:string"/>
<xsd:element name="ZIP" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="First Name"/>

<!-- The schema goes on and on and on... -->
</xsd:schema>

The result I'm hoping for is:

<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Element-1">
<xsd:complexType>
<xsd:sequence id="01">
<xsd:element name="ID" type="xsd:integer" default="0"/>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Element-2">
<xsd:complexType>
<xsd:sequence id="02">
<xsd:element name="ID" type="xsd:integer" default="0"/>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Basically, I think that XSL and Xquery is the way to go. I know some of the basics (e.g. matching [@name='Element-1'] and using filters like ancestor-or-self and descendant), but I'm having a bit of trouble putting them all together.

Any ideas?

An "advanced" question that isn't as important, but may be interesting:

After figuring out the above, is there a way to parameterize this functionality? For example, if I'd like to make a subset schema with 10-20 different elements, is there a way of encapsulating the "template" command instead of copying it 10-20 times and making the string substitution?

Postnext
James DurningSubject: How can I make a subset Schema (XSD) using XSL/XQuery in Stylus Studio?
Author: James Durning
Date: 02 Oct 2007 03:33 PM
I would suggest something like the following:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:my="internal">
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="xs:element">
<xsl:if test="@name = document('')//my:names/name">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
<my:names>
<name>Element-1</name>
<name>Element-2</name>
</my:names>
</xsl:stylesheet>

Notice the list of element names at the end.
The elements that match the list are copied, whereas those that do not are not.

Postnext
Matthew FadoulSubject: How can I make a subset Schema (XSD) using XSL/XQuery in Stylus Studio?
Author: Matthew Fadoul
Date: 03 Oct 2007 05:00 PM
Hello James,

Thank you for your example code. I've made some tweaks, and here's where I'm currently at:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<!-- Looking at immediate children of the root node. -->
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<!-- Prototype Template -->
<xsl:template name="MySimpleTemplate">
<xsl:copy-of select="."/>
</xsl:template>

<!-- Using the Prototype Template -->
<!-- I had to explicitly match each of these nodes. -->
<xsl:template match="xs:element[@name='Element-1']">
<xsl:call-template name="MySimpleTemplate"/>
</xsl:template>

<xsl:template match="xs:element[@name='Element-2']">
<xsl:call-template name="MySimpleTemplate"/>
</xsl:template>

<!-- Added this template to remove some whitespace. -->
<xsl:template match="node()"/>
</xsl:stylesheet>

Some comments:

1) Encapsulated Templates

I mentioned this at the end of my original post. I was looking for named templates, which I use above.

2) <my:names> Data Structure

I thought this was a clever solution; but, I couldn't get it to work on my machine. The XSL couldn't seem to find the "my:names" node. I was thinking that the code (as written) might not automatically iterate through the list of names...maybe I'm wrong, though. Maybe an "xsl:for-each" element would be appropriate, if I could figure out how to access the "my:names" node.

3) XPath Axes

The current solution relies on the templates to search the immediate children of the root node. Unfortunately, I'm also looking for nodes that are deeper in the hierarchy. In particular, I'd like to find and preserve at some reference elements (e.g. <xs:element ref="Element-1"/>). I wanted to use axes such as "ancestor" and "descendant" to get to these nodes, while preserving the rest of the hierarchy. Basically, I want to preserve any node that is an ancestor or descendant or itself one of the nodes I specify. There is a hard code workaround I can try to get to most of the reference nodes, but I was hoping that the XPath Axes could provide an elegant solution.

4) XSL Mapper Error.

When I click on the "mapper" pane for the XSL file, I get the error, "Mapper data population failed." I did a Google search, and apparently this can happen when the XSL uses multiple namespaces. Any further advice to work around this?

5) White Space.

I added the last template to remove some of the whitespace. It actually is a bit overkill (removing more than I'd like), but Stylus Studio's auto-format button gives me an easy solution.

I've got a pretty bad case of new-useritis, so I appreciate all the help from the forum community!

Postnext
(Deleted User) Subject: How can I make a subset Schema (XSD) using XSL/XQuery in Stylus Studio?
Author: (Deleted User)
Date: 05 Oct 2007 12:08 PM
Hi Matthew,
the "Data population failed" error is a generic error, shown when Mapper fails to parse the XPath expressions in the XSL. However, I could not reproduce it using the fragments you posted. Could you post the code that triggers this error?

Thanks,
Alberto

Posttop
Matthew FadoulSubject: How can I make a subset Schema (XSD) using XSL/XQuery in Stylus Studio?
Author: Matthew Fadoul
Date: 09 Oct 2007 08:32 PM
Just a little update. When started from scratch and tried to recreate the Mapper error, Stylus Studio didn't complain when I clicked on the Mapper tab. So no more problem with the Mapper tab. :)

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.