Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using XSLT, how to get a certain token from a String list?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Dear all,

I have an XML file (part of it) is similar to this:

<data>1 2.1 3.5 4 6 12.0</data>

Now, I am writing an xslt to transform an xml file. I want to write a xslt to get the certain token from the above xml data.

As an example:

xsl:getToekenData($string, $position)

if $string is &quot;1 2.1 3.5 4 6 12.0&quot;, and the position is 2, it will return 2.1. But if the position specified is 7, it will return &quot;null&quot;, as the total number of token in the string is 6.

Can any one tell me how to implement it.

It would be very appreciated if some one can drop me some code here.

Many thanks.
 
there is a way to do what you're looking for. i made this example using ASP and the MSXML 3.0 parser doing just what flumpy said, creating a template that'll get the token values.

the XML i'm using for this:
Code:
<?xml version=&quot;1.0&quot; ?>
<Response>
  <ReturnData>
    <data>1 2.1 3.5 4 6 12.0</data>
  </ReturnData>
  <ReturnData>
    <data>5 2.1 3.5 8 6 12.0</data>
  </ReturnData>
  <ReturnData>
    <data>3.2 7 3.5 1.1 15 2.0</data>
  </ReturnData>
</Response>
the XSL:
Code:
<!-- ************************************************ -->
<table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot; width=&quot;650&quot;>
  <tr>
    <td valign=&quot;top&quot;>
      <xsl:for-each select=&quot;//data&quot;>
        Original Data: <xsl:value-of select=&quot;.&quot;/><br/><br/>
        <xsl:call-template name=&quot;output-tokens&quot;>
          <xsl:with-param name=&quot;list&quot; select=&quot;.&quot;/>
        </xsl:call-template>
        <hr/>
      </xsl:for-each>
    </td>
  </tr>
</table>
<!-- ************************************************ -->

<xsl:template name=&quot;output-tokens&quot;>
  <xsl:param name=&quot;list&quot; />
  <xsl:variable name=&quot;nlist&quot; select=&quot;concat(normalize-space($list),' ')&quot;/>
  <xsl:variable name=&quot;first&quot; select=&quot;substring-before($nlist, ' ')&quot;/>
  <xsl:variable name=&quot;rest&quot; select=&quot;substring-after($nlist, ' ')&quot;/>
  <xsl:value-of select=&quot;$first&quot;/>
  <xsl:if test=&quot;$rest&quot;>
    <xsl:text> | </xsl:text>
    <xsl:call-template name=&quot;output-tokens&quot;>
      <xsl:with-param name=&quot;list&quot; select=&quot;$rest&quot;/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>
the result is:

--------------------------------------------------------------------------------
Original Data: 1 2.1 3.5 4 6 12.0

1 | 2.1 | 3.5 | 4 | 6 | 12.0
--------------------------------------------------------------------------------
Original Data: 5 2.1 3.5 8 6 12.0

5 | 2.1 | 3.5 | 8 | 6 | 12.0
--------------------------------------------------------------------------------
Original Data: 3.2 7 3.5 1.1 15 2.0

3.2 | 7 | 3.5 | 1.1 | 15 | 2.0
--------------------------------------------------------------------------------

you modify the &quot;list&quot; param coming into the template by normalizing space (removing any extra spaces that may appear between values) and adding a space to the end of the value. this assures that the template works properly separating values and when the list contains a single token.

the
Code:
substring-before
assigns the first token to &quot;first&quot; and the remaining unparsed tokens are assigned to &quot;rest&quot; by
Code:
substring-after
. the template then calls itself again, passing the new param &quot;rest&quot;. the template will repeat through the token string until no more spaces are found.

i based this example on code found in Wrox Press's XSLT Programmer's Reference by Michael Kay (ISBN 1861003129). fantastic book.

&quot;Until you have the courage to lose sight of the shore, you will not know the terror of being forever lost at sea.&quot; - a very wise man
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top