RE: [xsl] Problem applying XSL to XML that uses a schema

Subject: RE: [xsl] Problem applying XSL to XML that uses a schema
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 9 Dec 2004 10:11:29 -0000
> 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>&#xA;</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.

Current Thread