Re: [xsl] Using JavaScript to access <xml:list> items

Subject: Re: [xsl] Using JavaScript to access <xml:list> items
From: "Agnes Kielen" <a.kielen@xxxxxxx>
Date: Wed, 17 Jul 2002 12:58:54 +0200
Hi Hugh,

> I have an XML file:
>
> <?xml version="1.0"?>
> <?xml-stylesheet type="text/xsl" href="ScriptTestQuest.xsl"?> <Root>
> <Lists> <List>1 3 4</List> <List>1 4 5</List> <List>5 3 1</List>
> </Lists> </Root>
>
> Which I am trying to transform with the xslt file:
>
> <?xml version="1.0"?>
> <xsl:stylesheet version="1.0"
>                 xmlns:xsl="http://www.w3.org/TR/WD-xsl"; > <xsl:script
> language="javascript"> <![CDATA[ function car (e) {
>    var strs = e.text.split(" ");
>    return parseInt(strs[0]);
> }

You are using an obsolute namespace only used by Microsoft.
The official one is <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"/>

See: http://www.netcrucible.com/ for more info.

In the namespace above  <xsl:eval> does not exist in stead you can use
something like:

<msxml:script  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
language="JScript">
    <!-- your script -->
</msxml:script>

But what result do you want because I get the feeling you don't need
JavaScript at all.
I you want to split the numbers this can be done with XSLT alone.
With this XML.
<Lists>
 <List>1 3 4</List>
 <List>1 4 5</List>
 <List>5 3 1</List>
</Lists>

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output method="xml"/>
<xsl:template match="/Lists">
 <nums><xsl:apply-templates/></nums>
</xsl:template>

<xsl:template match="List">
 <xsl:call-template name="Split">
  <xsl:with-param name="strInput" select="."/>
 </xsl:call-template>

</xsl:template>

<xsl:template name="Split">
 <!--This template will recursively break apart a comma-delimited string
into child elements-->
 <xsl:param name="strInput"/>
 <xsl:param name="strDelimiter" select="' '"/>
 <xsl:variable name="strNextItem" select="substring-before($strInput,
$strDelimiter)"/>
 <xsl:variable name="strOutput" select="substring-after($strInput,
$strDelimiter)"/>
 <xsl:variable name="strLen" select="string-length($strNextItem)"/>
 <xsl:choose>
  <xsl:when test="contains($strInput,$strDelimiter)">
  <num>
   <xsl:value-of select="$strNextItem"/>
  </num>
  <!-- At this point, the template will recursively call itself until the
last comma is found -->
  <xsl:call-template name="Split">
   <xsl:with-param name="strInput" select="$strOutput"/>
   <xsl:with-param name="strDelimiter" select="$strDelimiter"/>
  </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
   <!-- The otherwise clause will be reached when a comma is not located
using contains() -->
   <num>
    <xsl:value-of select="$strInput"/>
   </num>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

</xsl:stylesheet>

I admit not the most easy XSLT but that the same is true for your
javascript.

Cheers,
Agnes





 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread