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 May 6th, 2009, 03:57 PM
Registered User
 
Join Date: May 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pulling out data -- very surprising results to a newbie...

Hi to XSLT-ers, and thanx to Wrox for hosting:

I will describe in detail below my problem, but basically I am trying to pull out data using XSLT / Xpath from an xml document in order to feed simple numbers into Gnuplot. I run xsltproc from the command line (well, a script) on a Linux machine, output those numbers into "traffic.dat", and graph the results by calling gnuplot from the same script.

Here is my input xml (snipped -- basically 24 hours of traffic data every 15 minutes):

Code:
<chart>
        <xaxis>
                <value xid="0">5/5/09 11:30 am</value>
                <value xid="1">5/5/09 11:45 am</value>
<...SNIP...>
                <value xid="92">5/6/09 10:30 am</value>
                <value xid="93">5/6/09 10:45 am</value>
        </xaxis>
        <graphs>
                <graph gid="1">
                        <value xid="0">15540.87</value>
                        <value xid="1">15184.37</value>
<...SNIP...>
                        <value xid="92">14764.03</value>
                        <value xid="93">15293.12</value>
                </graph>
        </graphs>
I would like just a tab delimited file (ideally with rectified ISO dates) like so:

2009-05-06_23:45 14764.03
<... ETC ...>

I would happily settle for just the y-values, throwing out the dates (I can reproduce them more easily than parse the non-iso formats -- grr...), like so:

14764.03
15293.12
....


I have tried the following XSLT file via xsltproc (see version output below):

Code:
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/chart/graphs/graph/value" >
<xsl:text>mvt=</xsl:text>
<xsl:value-of select="text()" />
</xsl:template>
</xsl:stylesheet>
run as:

Code:
$ xsltproc traffic.xslt traffic.xml
I thought I would get :

mvt=15540.87
... ETC ...

but I get:

Code:
<?xml version="1.0"?>


                5/5/09 11:30 am
                5/5/09 11:45 am
<... SNIP ...>
                5/6/09 10:30 am
                5/6/09 10:45 am

<!- note  x axis values and whitespace -- I don't really want either!  -->

                        mvt=15540.87
                        mvt=15184.37
<... SNIP ...>
                        mvt=14764.03
                        mvt=15293.12

Output from xsltproc on version:

xsltproc --version <2335>
Using libxml 20626, libxslt 10117 and libexslt 813
xsltproc was compiled against libxml 20626, libxslt 10117 and libexslt 813
libxslt 10117 was compiled against libxml 20626
libexslt 813 was compiled against libxml 20626


If someone can give me the magic recipe for simple numerical output that would be fantastic.

Here is an xmlstarlet (not available on the target machine) invocation that works:

$ xmlstarlet sel -T -t -m /chart/graphs/graph/value -v text() -n traffic.xml

Thanks to all!
 
Old May 6th, 2009, 04:36 PM
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

The default template rule for a node, if there is no matching user-specified rule, is:

(a) for an element, apply templates to its children

(b) for a text node, copy the text to the output.

If you want to skip a whole subtree, the easiest way is probably to add the rule

Code:
<xsl:template match="xaxis"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old May 6th, 2009, 05:45 PM
Registered User
 
Join Date: May 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post

If you want to skip a whole subtree, the easiest way is probably to add the rule

Code:
<xsl:template match="xaxis"/>
That works beautifully (thanks!) except for the following output at the top:

Code:
<?xml version="1.0"?>
This may be xsltproc specific, but since I got an answer so quickly I thought I would try anyway.

Tx
 
Old May 7th, 2009, 12:09 PM
Registered User
 
Join Date: May 2009
Posts: 8
Thanks: 6
Thanked 0 Times in 0 Posts
Default

i dont know how to achieve with xslt but i did wth string manipulation with a split()

new_result = result.to_s.split('?>')[1]
 
Old May 7th, 2009, 12:19 PM
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

If you want text output then use
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
<xsl:output method="text"/>
...
</xsl:stylesheet>
in your XSLT stylesheet. The default output method is XML so that is why you get an XML declaration at the start.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog




Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Not Pulling Sibling Data? BobSSC XSLT 4 April 23rd, 2009 01:46 PM
SSIS Not pulling data correctly chevy SQL Server 2005 0 February 26th, 2008 04:12 PM
pulling data from webpage zeeonline XSLT 3 July 27th, 2006 03:04 PM
placeholder controls -- pulling data Fubar ASP.NET 1.0 and 1.1 Professional 6 September 8th, 2005 09:08 AM
Check boxe pulling data Calibus Classic ASP Databases 3 August 7th, 2004 02:11 AM





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