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 April 2nd, 2010, 02:37 PM
Authorized User
 
Join Date: Apr 2008
Posts: 11
Thanks: 5
Thanked 0 Times in 0 Posts
Default An Approach to Dynamic Schema-Aware Decision Making?

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:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:test="http://www.example.com/test"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.example.com/test input.xsd">
 <union type="a" x="123" y="456" z=""/>
 <union type="b" x=""    y="456" z="789"/>
 <union type="b" x=""    y=""    z="0"/>
</root>
And produce the following:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<results xmlns:xs="http://www.w3.org/2001/XMLSchema"
         xmlns:xslo="http://www.w3.org/1999/XSL/TransformAlias"
         xmlns:fn="http://www.w3.org/2005/xpath-functions"
         xmlns:test="http://www.example.com/test">
 <a xmlns="http://www.example.com/test" x="123" y="456"/>
 <b xmlns="http://www.example.com/test" y="456" z="789"/>
 <b xmlns="http://www.example.com/test" y=""    z="0"/>
</results>
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:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:xslo="http://www.w3.org/1999/XSL/TransformAlias"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:test="http://www.example.com/test"
  version="2.0">
  <xsl:output  method="xml" indent="yes" omit-xml-declaration="no"/>
 
  <xsl:import-schema schema-location="input.xsd" namespace="http://www.example.com/test"/>
      </xsl:variable>
      <xsl:element name="{@type}" namespace="http://www.example.com/test">
         <xsl:for-each select="@*">
            <xsl:variable name="testAttr">
               <xsl:text>test:</xsl:text><xsl:value-of select="name(.)" />
            </xsl:variable>
            <xsl:if test="$testObj//schema-attribute(test:q)">
               <xsl:attribute name="{name()}">
                  <xsl:value-of select="." />
               </xsl:attribute>
            </xsl:if>
         </xsl:for-each>
         <xsl:attribute name="TheEnd">
            <xsl:value-of select="." />
         </xsl:attribute>
      </xsl:element>
 </xsl:template>
</xsl:stylesheet>
Any ideas on how to approach this? Can this not be done in straight XSL?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Best approach for dynamic form hacking_mike HTML Code Clinic 2 March 21st, 2010 06:43 AM
XSLT decision Making javaguy1007 XSLT 1 April 7th, 2009 06:28 AM
Article: JavaScript Best Practices: Performance - Be Scope-Aware jminatel BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 0 January 29th, 2009 11:04 PM
Implementation Decision reckon ASP.NET 1.x and 2.0 Application Design 2 June 28th, 2004 11:04 AM
Design decision on class library projects? tkubaska BOOK: ASP.NET Website Programming Problem-Design-Solution 2 May 19th, 2004 11:43 AM





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