[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Problem applying XSL to XML that uses a schema

Subject: RE: Problem applying XSL to XML that uses a schema
From: Mukul Gandhi <mukul_gandhi@xxxxxxxxx>
Date: Thu, 9 Dec 2004 02:35:14 -0800 (PST)
applying xsl stylesheet
Hi Mike,
  I thought, Schema Aware XSLT processor was
neccessary in this case. But from your reply, it seems
it is not required in this case.. Can you please tell,
under what cases Schema Aware processor is required..?

Regards,
Mukul

--- Michael Kay <mike@xxxxxxxxxxxx> wrote:

> > 
> > I am trying to apply an XSL stylesheet to an XML
> file that 
> > uses a schema.
> > I will discuss first what does work.
> > 
> > The following is the input XML file (XML1):
> > <?xml version="1.0" encoding="UTF-8" ?>
> > <!-- MainComment1 -->
> > <mainelement>
> > 
> >    <!-- SubComment1.1 -->
> >    <subelement name="sub1">content</subelement>
> >    <!-- SubComment1.2 -->
> > 
> >    <!-- SubComment2.1 -->
> >    <subelement name="sub2">content</subelement>
> >    <!-- SubComment2.2 -->
> > 
> > </mainelement>
> > <!-- MainComment2 -->
> > 
> > 
> > The following is the xsl I have so far (XSL1):
> > <?xml version="1.0" encoding="UTF-8"?>
> > <xsl:stylesheet version="1.0" 
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> > <xsl:output method="xml" version="1.1"
> indent="yes" />
> > 
> > <xsl:template match="comment()">
> >   <xsl:comment><xsl:value-of
> select="."/></xsl:comment>
> > </xsl:template>
> > 
> > <xsl:template match="subelement|@*">
> >     <xsl:copy>
> >         <xsl:apply-templates select="node()|@*"/>
> >     </xsl:copy>
> > </xsl:template>
> > 
> > <xsl:template match="mainelement|@*">
> >   <xsl:copy>
> >       <xsl:apply-templates select="node()|@*"/>
> > 
> >       <xsl:comment >NewSubComment1.1</xsl:comment>
> >       <xsl:text>
</xsl:text>
> >       <subelement
> name="newsub">content</subelement>
> >       <xsl:comment >NewSubComment1.2</xsl:comment>
> >   </xsl:copy>
> > </xsl:template>
> > </xsl:stylesheet>
> 
> Both your template rules match the same attribute
> nodes, which is
> technically an error, though many processors will
> take the recovery action,
> which is to use the second template rule. Both
> template rules do the same
> thing with attributes, so this isn't a big problem,
> but it's untidy.
> > 
> > 
> > The following is the output from applying XSL1 to
> XML1 (OUTPUT1):
> > <?xml version="1.0" encoding="UTF-8"?>
> > <!-- MainComment1 -->
> > <mainelement>
> > 
> >    <!-- SubComment1.1 -->
> >    <subelement name="sub1">content</subelement>
> >    <!-- SubComment1.2 -->
> > 
> >    <!-- SubComment2.1 -->
> >    <subelement name="sub2">content</subelement>
> >    <!-- SubComment2.2 -->
> > 
> > <!--NewSubComment1.1-->
> > <subelement name="newsub">content</subelement>
> > <!--NewSubComment1.2-->
> > 
> > </mainelement>
> > <!-- MainComment2 -->
> > 
> > 
> > This is exactly what I want without the schema
> (OUTPUT1).  
> > The problem is when
> > the XML file uses a schema which is my real world
> problem.
> > 
> > The following is the input XML file that uses the
> schema (XML2):
> > <?xml version="1.0" encoding="UTF-8" ?>
> > <!-- MainComment1 -->
> > <mainelement
> >     xmlns="http://www.mydomain.com/mainelement"
> >    
>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >    
>
xsi:schemaLocation="http://www.mydomain.com/mainelement
> > file:///C:/schemas/mainelement.xsd">
> > 
> >    <!-- SubComment1.1 -->
> >    <subelement name="sub1">content</subelement>
> >    <!-- SubComment1.2 -->
> > 
> >    <!-- SubComment2.1 -->
> >    <subelement name="sub2">content</subelement>
> >    <!-- SubComment2.2 -->
> > 
> > </mainelement>
> > <!-- MainComment2 -->
> > 
> > 
> > When I apply XSL1 to XML2 I get the following
> output (OUTPUT2):
> > <?xml version="1.0" encoding="UTF-8"?>
> > <!-- MainComment1 -->
> > 
> >    <!-- SubComment1.1 -->
> >    content
> >    <!-- SubComment1.2 -->
> > 
> >    <!-- SubComment2.1 -->
> >    content
> >    <!-- SubComment2.2 -->
> > 
> > <!-- MainComment2 -->
> > 
> >
> 
> It's not the presence of the schema that has
> scuppered this, it's the
> presence of the default namespace declaration
> xmlns="http://www.mydomain.com/mainelement", which
> has changed the names of
> all your elements and means that they no longer
> match the rules in your
> stylesheet. To match an element with localname
> "mainelement" and namespace
> URI http://www.mydomain.com/mainelement, you need a
> template rule of the
> form
> 
> <xsl:template match="x:mainelement" 
>              
> xmlns:x="http://www.mydomain.com/mainelement">
> 
> Michael Kay
> http://www.saxonica.com/
> 
> 
> > 
> > 
> > 
> > OUTPUT2 is clearly not what I want.  I want the
> exact same 
> > output as OUTPUT1
> > except with the schema location and xmlns etc. 
> The following 
> > is the desired
> > output (DESIREDOUTPUT):
> > <?xml version="1.0" encoding="UTF-8"?>
> > <!-- MainComment1 -->
> > <mainelement
> >     xmlns="http://www.mydomain.com/mainelement"
> >    
>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> >    
>
xsi:schemaLocation="http://www.mydomain.com/mainelement
> > file:///C:/schemas/mainelement.xsd">
> > 
> >    <!-- SubComment1.1 -->
> >    <subelement name="sub1">content</subelement>
> >    <!-- SubComment1.2 -->
> > 
> >    <!-- SubComment2.1 -->
> >    <subelement name="sub2">content</subelement>
> >    <!-- SubComment2.2 -->
> > 
> > <!--NewSubComment1.1-->
> > <subelement name="newsub">content</subelement>
> > <!--NewSubComment1.2-->
> > </mainelement>
> > <!-- MainComment2 -->
> > 
> > 
> > 
> > 
> > 
> > Can anyone help me understand how to modify my
> XSL1 to create 
> > the DESIREDOUTPUT?
> >  Any help would be greatly appreciated.  I have
> been trying 
> > to make this work
> > for days!
> > 
> > 
> > Thanks,
> > 
> > Tim
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> >
>
----------------------------------------------------------------
> > This message was sent using IMP, the Internet
> Messaging Program.
> 
> 



		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - Helps protect you from nasty viruses. 
http://promotions.yahoo.com/new_mail

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.