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 February 11th, 2009, 07:20 AM
Authorized User
 
Join Date: Feb 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default XSLT Returns Too Much Data - Potentially Namespace Related

Hello...

I'm trying to transform a complex xml document to a text file and I keep getting a lot of extra data in my output. The following is a sample of the xml:

Code:
 
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <env:Header/>
 <env:Body>
  <m:getResponse xmlns:m="temp.temp1.temp2">
   <n1:data_result xmlns:n1="temp.temp1.temp2">
    <n2:aircraftModels xmlns:n2="java:temp.temp1.temp2.value" xsi:type="n2:ArrayOfAircraftModel">
     <n2:AircraftModel xsi:type="n2:AircraftModel">
      <n2:aircraftType>A1</n2:aircraftType>
     </n2:AircraftModel>
     <n2:AircraftModel xsi:type="n2:AircraftModel">
      <n2:aircraftType>A10</n2:aircraftType>
     </n2:AircraftModel>
    </n2:aircraftModels>
    <n3:fUnits xmlns:n3="java:temp.temp1.temp2.value" xsi:type="n3:ArrayOfFUnit"/>
    <n4:operatingLocations xmlns:n4="java:temp.temp1.temp2.value" xsi:type="n4:ArrayOfOperatingLocation">
     <n4:OperatingLocation>
      <n4:availabilities xsi:type="n4:ArrayOfAvailability"/>
      <n4:geodetic>
       <n5:datum xmlns:n5="java:temp.temp1.temp2.conversion">WRS84</n5:datum>
       <n6:height xmlns:n6="java:temp.temp1.temp2.conversion">0.0</n6:height>
       <n7:latitude xmlns:n7="java:temp.temp1.temp3.conversion">1234567890</n7:latitude>
       <n8:longitude xmlns:n8="java:temp.temp1.temp3.conversion">-1234567890</n8:longitude>
      </n4:geodetic>
     </n4:OperatingLocation>
   </n1:data_result>
  </m:getResponse>
 </env:Body>
</env:Envelope>
I have no control over the formatting of this data or the way namespaces are used within it. Here's my xslt:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:fn="http://www.w3.org/2005/xpath-functions"
           xmlns:n2="java:temp.temp1.temp2.value">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="n2:aircraftModels">
  <xsl:for-each select="n2:AircraftModel">
   <xsl:value-of select="n2:aircraftType"/><xsl:text>
</xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
And here's the result:

Code:
A1
A10
WRS840.01234567890-1234567890
As you can see the result is not what I was expecting. I'm thinking that the problem may be due to the namespaces or maybe the xsi:type attributes in the source xml. Thanks for any help you can provide!


Oz
 
Old February 11th, 2009, 07:30 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You don't actually say what output you want.

The default template rule copies all the text of an element to the output. I think you want a default that doesn't copy the text. You can achieve this with the rule

<xsl:template match="*"/>

This has nothing to do with namespaces.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old February 11th, 2009, 08:42 AM
Authorized User
 
Join Date: Feb 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default Reply

In this case I was just looking for a list of each aircraftType followed by a CR/LF so I expected to just see the following:

A1
A10


Instead it just keeps dumping all the text data found after the closing </n2:AircraftModel> tag. I'm using XMLSpy and I'll try your suggestion and let you know how it works. Thanks


Oz
 
Old February 11th, 2009, 09:01 AM
Authorized User
 
Join Date: Feb 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hmmm... I tried:
Code:
<xsl:template match="*">
  <xsl:for-each select="n2:AircraftModel">
   <xsl:value-of select="n2:aircraftType"/><xsl:text>
</xsl:text>
  </xsl:for-each>
 </xsl:template>
and it didn't return anything....

Last edited by Ozeroc; February 11th, 2009 at 09:07 AM..
 
Old February 11th, 2009, 09:20 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

It's not supposed to, it's a catch-all that means that any elements not matched specifically are to be ignored:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:fn="http://www.w3.org/2005/xpath-functions"
           xmlns:n2="java:temp.temp1.temp2.value">
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>
 <xsl:template match="n2:aircraftModel">  
   <xsl:value-of select="n2:aircraftType"/><xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
__________________
Joe
http://joe.fawcett.name/
 
Old February 11th, 2009, 09:35 AM
Authorized User
 
Join Date: Feb 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Huh... That make's sense. But when I add that as you indicated above, I don't get any output whatsoever. O.o


Oz
 
Old February 11th, 2009, 09:37 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Try:
Code:

<xsl:stylesheetversion="2.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:n2="java:temp.temp1.temp2.value">
<xsl:outputmethod="text"/>
<xsl:strip-spaceelements="*"/>
<xsl:templatematch="/">
<xsl:apply-templatesselect="//n2:AircraftModel" />
</xsl:template>
<xsl:templatematch="n2:AircraftModel">
<xsl:value-ofselect="n2:aircraftType"/>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:templatematch="*"/>
</xsl:stylesheet>
__________________
Joe
http://joe.fawcett.name/
 
Old February 11th, 2009, 09:57 AM
Authorized User
 
Join Date: Feb 2009
Posts: 13
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Wow! That completely worked. I guess I need to try to retrain my brain for dealing with XML. I so thought a for-each loop was the way to go here. Thanks for the help.


Oz





Similar Threads
Thread Thread Starter Forum Replies Last Post
Default Namespace - XSLT 1.0 Geierwally XSLT 2 July 9th, 2007 11:08 AM
XSLT related exception prashant.unleashed BOOK: XSLT Programmer's Reference, 2nd Edition 15 June 7th, 2007 04:30 AM
XSLT related exception prashant.unleashed J2EE 0 May 3rd, 2007 04:44 AM
problem related to XSLT prashant.unleashed BOOK: Expert One-on-One J2EE Design and Development 0 May 3rd, 2007 03:42 AM
dynamic xslt -> xslt creation namespace problem jkmyoung XSLT 2 July 15th, 2006 12:42 AM





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