Re: [xsl] Splitting Data into Two Sets, iterating over one

Subject: Re: [xsl] Splitting Data into Two Sets, iterating over one
From: "Abel Braaksma (online)" <abel.online@xxxxxxxxx>
Date: Thu, 21 Jun 2007 10:35:56 +0200 (CEST)
> <nodes>
>   <node value="name1 arbitrary_data_string1">
>   <node value="name2 arbitrary_data_string2">
>   ...
> </nodes>
>
>
> Ideally, the data would just be in separate attributes but this is
> not what the format is...
>
> If I had the data seperated as <item name="name" value="value"> I'd
> know what to do now, use distinct-values($files/nodes/node/@name)
to get
> a list of all possible names, then use the list of names to retrieve
> the values for the table.
>
> Now however, the name and corresponding data are combined.

You can use something like this:

<xsl:variable name="split-nodes">
   <xsl:for-each select="$files/nodes/node">
       <node
          name="{tokenize(., ' ')[1]}"
          value="{tokenize(., ' ')[2]}" />
   </xsl:for-each>
</xsl:variable>

<xsl:apply-templates select="$split-nodes/node" />

....

<xsl:template match="node">
    .... do something with @name or @value ...
</xsl:template>


> I figured out how to extract the data name/value pairs from
> the string using the
> <xsl:analyze-string> and a regex. (analyze-string doesn't seem to
> take in multiple values, I tried passing $files/nodes/node/@name )

What do you mean with 'takes multiple values'? xsl:analyze-string
can analyze a string. Which means that if you give it a sequence of
multiple values, the values will be concatenated using default
normalization rules. It is better to use template matching on @name
and apply your analyze-string inside the match:

<xsl:template match="@name">
   <xsl:analyze-string ....>
    .... etc
</xsl:template>

But I don't see why you would use the analyze-string instruction. It
is meant for more complex string analyzing that cannot be done using
tokenize() and replace().

> However, I
> don't know how to somehow store this as an array ("sequence"?) or
> associate these new separate data lists so I can do a name->value
> lookup.

An array is not available in XSLT. But you can create a sequence of
any type, which 'feels' pretty much like an array. Oh, and you
cannot store anything in XSLT either ;)

>
> Once I have a list of all possible unique "names", I would like to
> iterate across this list and select the corresponding "value" from
> each file for the table (or, again 'N/A' if not present).

Either apply-templates on the variable containing the temporary
tree, or xsl:for-each if it is just a sequence of distinct (but
distinction will loose your 'N/A' nodes) strings.

>
> It'd be ideal if I could somehow make it look just like <item
> name="name" value="value"> somehow since the rest of my code
> supports this format already,

Create a variable that creates a temporary tree that looks like the
format you want and go from there (this technique is called micro
pipelining). See above, where I gave you an example of how to do
this with your input.

Hope this helps.

Happy coding, cheers!
-- Abel Braaksma

Current Thread