Re: [xsl] Defining variables as sequence of strings

Subject: Re: [xsl] Defining variables as sequence of strings
From: "Dimitre Novatchev dnovatchev@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 14 May 2015 18:26:28 -0000
Joseph,

> One question though, in my effort to refactor and avoid the for-each
>
> constructs, how do you output the results of the sequence contained
>
> within $vTypes utilizing some type formatting while inside a perform-sort.
>
> For example, I want each element on a line of its own, contained within
>
> some other text:
>
>
>
> ......Some other text (a) More text....
>
> ......Some other text (b) More text....
>
> ......Some other text (c) More text....
>

Depending on the particular "formatting task" this can be done in
different ways -- even using <xsl:foreach>

Here is a simple example that can replace the sorting code in the
previous solution and that produces your wanted output:

    <xsl:variable name="vSorted" as="xs:string*">
     <xsl:perform-sort select="distinct-values($vTypes)">
      <xsl:sort select="."/>
     </xsl:perform-sort>
     </xsl:variable>

     <xsl:value-of select=
      "$vSorted/concat('...Some other text (', ., ') More Text....')"
separator="&#xD;&#xA;"/>


So, the complete transformation in this case is:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:xs="http://www.w3.org/2001/XMLSchema";>
  <xsl:output method="text"/>
  <xsl:template match="/*">
    <xsl:variable name="vTypes" as="xs:string*">
      <xsl:apply-templates select="*/@type"/>
    </xsl:variable>
    <xsl:variable name="vSorted" as="xs:string*">
     <xsl:perform-sort select="distinct-values($vTypes)">
      <xsl:sort select="."/>
     </xsl:perform-sort>
     </xsl:variable>

     <xsl:value-of select=
      "$vSorted/concat('...Some other text (', ., ') More Text....')"
separator="&#xD;&#xA;"/>
  </xsl:template>
  <xsl:template match="property/@type[. eq 'foo']">
    <xsl:sequence select="'a', 'b'"/>
  </xsl:template>
  <xsl:template match="property/@type[. = ('bar', 'baz', 'bof')]">
    <xsl:sequence select="'b', 'c'"/>
  </xsl:template>
  <xsl:template match="property/@type" priority="-1">
   <xsl:sequence select="'c', 'd'"/>
  </xsl:template>
 </xsl:stylesheet>


and when applied on the same XML document:

<object>
   <property name="name" type="foo"/>
   <property name="key" type="bar"/>
   <property name="size" type="baz"/>
   <property name="age" type="bof"/>
   <property name="grade" type="goo"/>
 </object>

it produces the wanted result:

...Some other text (a) More Text....
...Some other text (b) More Text....
...Some other text (c) More Text....
...Some other text (d) More Text....

Instead of the function concat(), you may call an xsl:function that
you have written yourself, that could perform more subtle formatting.


Cheers,
Dimitre




-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread