xsl-list
[Top] [All Lists]

Re: Problem applying XSL to XML that uses a schema

2004-12-09 02:21:22
Hi Tim,

The problem is that you have two different input documents. In the first case that works all the elements are in no namespace while in the second case that does not work all the elements are in the http://www.mydomain.com/mainelement namespace. When you use an element name in an XPath expression without specifying a prefix that is considered from no namespace. In order to match your elements from the http://www.mydomain.com/mainelement namespace you need to map a prefix to this namespace and use that when you refer to elements from that namespace, something like below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
    xmlns:t="http://www.mydomain.com/mainelement";>
    <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="t:subelement|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="t: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>

Best Regards,
George
---------------------------------------------------------------------
George Cristian Bina
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com


tim(_at_)reatech(_dot_)com wrote:
Hello,

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>


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 -->





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.


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--