Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
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 July 12th, 2008, 08:43 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why data-type="number" not working here...

Is it because I am returning the node and not the text data?

Input
=====

<?xml version="1.0" encoding="UTF-8"?>
<sqladapterresponse>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age1</column>
<column name="field3">Value 1</column>
<column name="SortOrder">1</column>
</row>
<row>
<column name="field1">Name2</column>
<column name="field2">Age2</column>
<column name="field3">Value 2</column>
<column name="SortOrder">5</column>
</row>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age3</column>
<column name="field3">Value 1</column>
<column name="SortOrder">4</column>
</row>
<row>
<column name="field1">Name4</column>
<column name="field2">Age4</column>
<column name="field3">Value 4</column>
<column name="SortOrder">2</column>
</row>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age4</column>
<column name="field3">Value 1</column>
<column name="SortOrder">3</column>
</row>
</sqladapterresponse>

XSLT
====

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">


<xsl:template match="sqladapterresponse">
<xsl:apply-templates>
<xsl:sort select="//column[@name='SortOrder']" data-type="number"/>
</xsl:apply-templates>
</xsl:template>


<xsl:template match="/">
<sqladapterresponse>
<xsl:copy-of select="//row[column[@name='field1']/text()='Worklog' and column[@name='field3']/text()='Value 1']"/>
</sqladapterresponse>
</xsl:template>
</xsl:stylesheet>

Output
======
<?xml version="1.0" encoding="utf-8"?>
<sqladapterresponse>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age1</column>
<column name="field3">Value 1</column>
<column name="SortOrder">1</column>
</row>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age3</column>
<column name="field3">Value 1</column>
<column name="SortOrder">4</column>
</row>
<row>
<column name="field1">Worklog</column>
<column name="field2">Age4</column>
<column name="field3">Value 1</column>
<column name="SortOrder">3</column>
</row>
</sqladapterresponse>

 
Old July 12th, 2008, 09:00 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't think the template matching sqladapterresponse is applied at all as the template matching the root node simply copies some row elements but does not use any apply-templates.
So instead of using xsl:copy-of you need to use e.g.
Code:
<xsl:template match="/">
<sqladapterresponse>
<xsl:apply-templates select="sqladapterresponse/row[column[@name='field1']/text()='Worklog' and column[@name='field3']/text()='Value 1']">
  <xsl:sort select="column[@name='SortOrder']" data-type="number"/>
</xsl:apply-templates>
</sqladapterresponse>
</xsl:template>
</xsl:stylesheet>
That way you sort the selected rows, then make sure you have a template for the 'row' elements that copies them to the result tree.


--
  Martin Honnen
  Microsoft MVP - XML
 
Old July 12th, 2008, 10:18 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

<xsl:sort select="//column[@name='SortOrder']"

A path expression starting with "//" selects from the root of the document tree. This path expression selects all the SortOrder columns in the document, and the sort key is taken as the first of these. Just leave out the "//".


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 12th, 2008, 10:41 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks to both of you!

Michael, I removed these // and got the same result.

So I used Martin's suggestion. I added below template for 'row' at the bottom of the template he did:

<xsl:template match="row">
<xsl:copy-of select="."/>
</xsl:template>

However, my editor displayed the result row each separated by a space line. I wonder why it happened. I can fix it with <xsl:strip-space elements="*"/> but I'm just curious why the sorting added those extra lines? Not something that bothers me at the moment but ...

Many thanks again!!


 
Old July 12th, 2008, 10:51 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Michael, I removed these // and got the same result.

That's because I only noticed one of your errors.

>However, my editor displayed the result row each separated by a space line.

Apply-templates by default selects all the children including elements and whitespace text nodes. Do select="*" or select="row" to avoid selecting them, or xsl:strip-space to remove them at source.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 12th, 2008, 11:00 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Unwanted white space is usually the result of some white space copied from the input to the output and/or of using xsl:output indent="yes".
With the template I suggested I don't think any white space from the input is copied to the output so I suspect you have an xsl:output indent="yes" in your stylesheet.
Which XSLT processor are you using within your editor?
When I run the complete stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">

<xsl:template match="/">
<sqladapterresponse>
<xsl:apply-templates select="sqladapterresponse/row[column[@name='field1']/text()='Worklog' and column[@name='field3']/text()='Value 1']">
  <xsl:sort select="column[@name='SortOrder']" data-type="number"/>
</xsl:apply-templates>
</sqladapterresponse>
</xsl:template>


<xsl:template match="row">
  <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>
against your sample input with Saxon 6.5.5 then I don't see any white space between row elements in the output, the result looks as follows:
Code:
<?xml version="1.0" encoding="utf-8"?><sqladapterresponse><row>
<column name="field1">Worklog</column>
<column name="field2">Age1</column>
<column name="field3">Value 1</column>
<column name="SortOrder">1</column>
</row><row>
<column name="field1">Worklog</column>
<column name="field2">Age4</column>
<column name="field3">Value 1</column>
<column name="SortOrder">3</column>
</row><row>
<column name="field1">Worklog</column>
<column name="field2">Age3</column>
<column name="field3">Value 1</column>
<column name="SortOrder">4</column>
</row></sqladapterresponse>
--
  Martin Honnen
  Microsoft MVP - XML
 
Old July 12th, 2008, 07:13 PM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael and Martin

Quote:
quote:Originally posted by mhkayApply-templates by default selects all the children including elements and whitespace text nodes. Do select="*" or select="row" to avoid selecting them, or xsl:strip-space to remove them at source.
I read sometime back that pure xpath aware processor will give me those spaces. Usually the xml output is the same all through out for the templates that I create until now that I have to sort my output and struggle at work and have to use a different editor at home.

Quote:
quote:Originally posted by Martin Honnen
 Unwanted white space is usually the result of some white space copied from the input to the output and/or of using xsl:output indent="yes".
With the template I suggested I don't think any white space from the input is copied to the output so I suspect you have an xsl:output indent="yes" in your stylesheet.
Which XSLT processor are you using within your editor?
When I run the complete stylesheet

Yes, using my editor at home I regularly add indent="yes" to check the output in tabbed structure (it is easier for me to scan the result). And my IDE at work does give me a way to quickly view the output by toggling on a switch without having to change and run the xslt to see the differences in the output.

Sorting allowed me to know what's really going on and thanks to both of you for the technical bit to watch out for when switching between editors or IDE.

My trialware editor at home is using Saxon 9.0.0.6

And now, please, can you guys enjoy your weekend already? You've been helping the community like 24x7...

Many thanks again for the usual support guys and more power!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Scale and Precision in number Data type code_lover Oracle 1 April 9th, 2009 04:56 PM
Error Number 13 : Type Mismatch ryoga_7482 BOOK: Expert Access 2007 Programming ISBN 978-0-470-17402-9 2 January 31st, 2008 02:20 AM
casting textbox to sqlserver number data type yoord BOOK: Beginning ASP.NET 1.0 1 October 17th, 2004 06:09 AM
Data Type error but it is the correct type Mitch SQL Server 2000 2 March 19th, 2004 11:31 AM
format total number..not working rissay Access 5 February 23rd, 2004 09:10 AM





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