Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old March 23rd, 2007, 06:30 AM
Registered User
 
Join Date: Mar 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default expanding default values in xslt

Hi,

Is it possible to expand default values in XSLT? I have the following XML Schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="Config">
  <xs:complexType>
    <xs:sequence>
        <xs:element name="Element1" type="xs:boolean" default="true"/>
        <xs:element name="Element2" type="xs:int"/>
        <xs:element name="Element3" default="YES">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:enumeration value="YES"/>
                    <xs:enumeration value="NO"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

And I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Config.xsd">
        <Element1>true</Element1>
        <Element2>123</Element2>
        <Element3/>
</Config>

Which when I put through the following stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="Config"/>

<xsl:template match="/Config/*">
  <xsl:value-of select="name()"/>
  <xsl:text>=</xsl:text>
  <xsl:value-of select="."/>
  <xsl:text>#xa;</xsl:text>
</xsl:template>

</xsl:stylesheet>

I get the following output:
Element1=true
Element2=123
Element3=

Is there anyway I can get XSLT to output the default value of YES for Element3 as defined in the schema? I am using C++ Xalan 1.10.

thanks
  Alex



  #2 (permalink)  
Old March 23rd, 2007, 06:39 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The obvious way to do this is to use a schema-aware XSLT 2.0 processor such as Saxon-SA. This will do a lot more than expanding default attributes, it will also enable you detect errors in your stylesheet code more quickly: an XSLT processor can also use type information for optimizing your code.

If all you want from your schema, however, is expansion of defaults then you may be able to arrange for the output of your schema processor to be piped into your XSLT 1.0 processor. For example I think this will happen if you implement the validation as a JAXP ValidatorHandler (but you're using C++, I don't know about that).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default Field Values tachtenberg Reporting Services 1 February 19th, 2008 05:52 PM
Default Namespace - XSLT 1.0 Geierwally XSLT 2 July 9th, 2007 11:08 AM
setting default values prithvi28 XSLT 1 October 3rd, 2006 01:38 PM
Default values? Raymie_C Access VBA 2 May 16th, 2005 11:48 AM
Default Values davest Access VBA 2 October 19th, 2004 05:57 AM





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