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 May 4th, 2007, 12:01 AM
Authorized User
 
Join Date: Apr 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default missing schemalocation in output XML

trying to do a transform and everything is working but I am losing the xsi:schemalocation entry in the output XML

Missing:
xsi:schemaLocation="http://www.ourbedandbreakfast.com/reserve_schema.xsd"

Original Input XML:

<?xml version="1.0" encoding="UTF-8"?>
<bb3:Reservations xmlns:bb3="http://www.ourbedandbreakfast.com/sample1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ourbedandbreakfast.com/reserve_schema.xsd">
    <bb3:Reservation RoomNum="106" InvoiceNum="BB3002">
        <bb3:Customer FirstName="Tom" LastName="Cruise" />
        <bb3:Customer FirstName="Nicole" LastName="Kidman" />
    </bb3:Reservation>
    <bb3:Reservation RoomNum="206" InvoiceNum="BB5010">
        <bb3:Customer FirstName="Tom" LastName="Cruise" />
    </bb3:Reservation>
    <bb3:Reservation RoomNum="506" InvoiceNum="BB6020">
        <bb3:Customer LastName="Madonna" />
    </bb3:Reservation>
</bb3:Reservations>

XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:bb3="http://www.ourbedandbreakfast.com/sample1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ourbedandbreakfast.com/reserve_schema.xsd">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:template match="bb3:Reservations">
  <bb3:CustomerVisits>
  <xsl:for-each-group select="bb3:Reservation/bb3:Customer" group-by="concat(@FirstName, '~', @LastName)">
         <bb3:Booking>
             <xsl:copy>
               <xsl:copy-of select="@*"/>
               <xsl:apply-templates mode="copy"/>
             </xsl:copy>
    <xsl:for-each select="current-group()">
      <InvoiceNum><xsl:value-of select="../@InvoiceNum"/></InvoiceNum>
    </xsl:for-each>
    </bb3:Booking>
  </xsl:for-each-group>
   </bb3:CustomerVisits>
</xsl:template>
</xsl:stylesheet>

Final Output:

<?xml version="1.0" encoding="UTF-8"?>
<bb3:CustomerVisits xmlns:bb3="http://www.ourbedandbreakfast.com/sample1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <bb3:Booking>
  <bb3:Customer FirstName="Tom" LastName="Cruise"/>
      <InvoiceNum>BB3002</InvoiceNum>
      <InvoiceNum>BB5010</InvoiceNum>
 </bb3:Booking>
 <bb3:Booking>
  <bb3:Customer FirstName="Nicole" LastName="Kidman"/>
      <InvoiceNum>BB3002</InvoiceNum>
 </bb3:Booking>
 <bb3:Booking>
  <bb3:Customer LastName="Madonna"/>
      <InvoiceNum>BB6020</InvoiceNum>
 </bb3:Booking>
</bb3:CustomerVisits>
 
Old May 4th, 2007, 01:13 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Putting an attribute on the xsl:stylesheet element won't cause it to be added to the result document.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 4th, 2007, 05:48 AM
Authorized User
 
Join Date: Apr 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This change seems to help, but the final output changes the order of the namespaces (xsi comes before bb3).

Why would that be happening, that is the ordering changing?

<xsl:template match="bb3:Reservations">
  <bb3:CustomerVisits xmlns:bb3="http://www.ourbedandbreakfast.com/sample1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ourbedandbreakfast.com/reserve_schema.xsd">
  <bb3:CustomerVisits>
  <xsl:for-each-group select="bb3:Reservation/bb3:Customer" group-by="concat(@FirstName, '~', @LastName)">
         <bb3:Booking>
             <xsl:copy>
               <xsl:copy-of select="@*"/>
               <xsl:apply-templates mode="copy"/>
             </xsl:copy>
    <xsl:for-each select="current-group()">
      <InvoiceNum><xsl:value-of select="../@InvoiceNum"/></InvoiceNum>
    </xsl:for-each>
    </bb3:Booking>
  </xsl:for-each-group>
   </bb3:CustomerVisits>
</xsl:template>

 
Old May 4th, 2007, 05:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

As with attributes, the order of namespace declarations within an element start tag has no significance and you can't control it.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 4th, 2007, 06:01 AM
Authorized User
 
Join Date: Apr 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay thats good to know.

Makes sense because oXygen using the Saxon8B engine with the same XSL does get the ordering right.

But my JAXP code using the saxon8b.jar does not. So my JUnit test fails when the output is compared to the expected file.

Its odd that using the same engine is producing different results though. Any thoughts?

 
Old May 4th, 2007, 06:27 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

No, it's not at all odd. Because order is important, the processor can use data structures such as hash tables that don't preserve order.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 4th, 2007, 06:29 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Because order is important
s/important/unimportant/

When comparing test results against reference results, consider (a) canonicalizing both first (see "Canonical XML"), or (b) using the XPath 2.0 deep-equals() function.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 4th, 2007, 07:55 AM
Authorized User
 
Join Date: Apr 2007
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you suggest a template that I could use to maybe copy all namespaces and schema locations from the original "<bb3:Reservations" and then place after "<bb3:CustomerVisits "

It seems that there is probably a better way then hard coding them.

Thanks for the tip on the test case comparisons, I will look into those suggestions.






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to see xml output in IE? anboss XSLT 1 August 29th, 2008 05:16 PM
schemaLocation vs nonamespaceschema... newbieboobers XML 0 August 28th, 2008 11:30 AM
problem with xsi:SchemaLocation akshay144 XML 2 August 20th, 2008 05:40 AM
missing CDATA in the output anboss XSLT 6 June 6th, 2008 10:15 AM
Output XML Hadware .NET Framework 1.x 0 December 28th, 2005 09:55 PM





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