XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> + Use of before and after string (3) Sticky Topic
-> - How do I substitute element ty... (1)
-> + How does one add working days ... (4)
-> - Help, I have existing XLT and... (1)
-> + Need help on XSLT issue - (2)
-> + EDI to XML Conversion (7)
-> - XML To JSON Conversion using X... (1)
-> + Formatting Paragraphs to same ... (2)
-> - Grouping of records (1)
-> + Problems with xsd 1.1 (4)
-> + XML to HL7 mapping (3)
-> + XSLT 3 and Iterate (2)
-> + XSL-FO to PDF preview (3)
-> + java.lang.RuntimeException: Er... (2)
-> + Create Acroforms with Stylus X... (2)
-> + How to change XSLT parameter s... (3)
-> + how to change format of the da... (2)
-> + Search "Next 8 Results " doesn... (2)
-> - Support for Git (1)
-> + newbee (8)
-- [1-20] [21-40] [41-60] Next
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
Topic  
Postnext
Mark KiefferSubject: Extracting specific comma separated data
Author: Mark Kieffer
Date: 24 Jul 2007 01:31 PM
Hi,
I have an xml with comma separated data, but I want to extract only specific values from these data. Here is the part of xml I am interested in:

<dataset xmlns="http://datafed.net/xs/Catalog">
<LatLonTimeCube>
<dimensions>
<parameter_dim dim_type="parameter">
<granules>
param_abbr, param_name, param_unit
CO, CO Total, gm-2s-1
CO2, CO2 Total, gm-2s-1
NO, NO Total, g.m-2s-1
NO2, NO2 Total, gm-2s-1
SO2, SO2 Total, gm-2s-1
</granules>
</parameter_dim>
</dimensions>
</LatLonTimeCube>
</dataset>

I would like the output to be
Parameters=CO, CO2, NO, NO2, SO2
I looked around and could only find information on extracting all the values in order.

Thanks,
Mark

Postnext
Minollo I.Subject: Extracting specific comma separated data
Author: Minollo I.
Date: 24 Jul 2007 04:59 PM
I think yout best option is to use XSLT 2 and the tokenize() function (http://www.w3.org/TR/xpath-functions/#func-tokenize)

If the parsing/conversion of the embedded text gets too complicated, you may want to consider extracting it from the XML and parsing it using XML Converters, for example (http://www.xmlconverters.com).

Postnext
Jan VerhoekSubject: Extracting specific comma separated data
Author: Jan Verhoek
Date: 24 Jul 2007 05:03 PM
With XSLT and the Tokenize function it could look like:

<xsl:template match="granules">
<xsl:variable name="gran" select="tokenize(., '\n')[position()>1]"/>

<xsl:if test="$gran[1]">
<xsl:text>Parameters: </xsl:text>
<xsl:for-each select="$gran">
<xsl:value-of select="tokenize(., ',')[1]"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>

</xsl:template>

If you're stuck to XSLT 1.0 try substring within a recursive function!

Posttop
Mark KiefferSubject: Extracting specific comma separated data
Author: Mark Kieffer
Date: 25 Jul 2007 01:39 PM
I think I am stuck in XSLT 1.0 for now. So I am working on the recursive function that you mentioned. I found a good starting point after a few searches, but I am still having trouble skipping the values in between the values I want. So far I have

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="granules">
<xsl:variable name="dataString" select="text()"/>
<xsl:call-template name="commaSplit">
<xsl:with-param name="dataString" select="$dataString"/>
<xsl:with-param name="position" select="1"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="commaSplit">
<xsl:param name="dataString"/>
<xsl:param name="position"/>
<xsl:choose>
<xsl:when test="contains($dataString,'param_abbr, param_name, param_unit')">
<!-- Select the first value to process -->
<xsl:call-template name="doWhatever">
<xsl:with-param name="doWith"
select="substring-before($dataString,'param_abbr, param_name, param_unit')"/>
<xsl:with-param name="position" select="$position"/>
</xsl:call-template>
<!-- Recurse with remainder of string -->
<xsl:call-template name="commaSplit">
<xsl:with-param name="dataString"
select="substring-after($dataString,'param_abbr, param_name, param_unit')"/>
<xsl:with-param name="position" select="$position + 1"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<!-- This is the last value so we don't recurse -->
<xsl:call-template name="doWhatever">
<xsl:with-param name="doWith" select="$dataString"/>
<xsl:with-param name="position" select="$position"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!-- Process of individual value here -->
<xsl:template name="doWhatever">
<xsl:param name="doWith"/>
<xsl:param name="position"/>
<outputValueInElementWithABigLongName position="{$position}">
<xsl:value-of select="$doWith"/>
</outputValueInElementWithABigLongName>
</xsl:template>

<xsl:template match="*">
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

This gets rid of the first 3 values, but I cannot figure anything else out.

   
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.