xsl-list
[Top] [All Lists]

Re: how to parse attribute value seperated by "|"

2004-04-20 02:00:03
Hi,

I have an element like this:

<para one-of="|11|22|33|44|"/>

I want to get the value list of its attribute"one-of", but I don't
know how to parse it,

In XSLT 1.0, to parse the one-of attribute, you need to use a
recursive template that parses the string by breaking it at each |.
Something like:

<xsl:template match="@one-of" name="parse-one-of">
  <xsl:param name="list" select="string(.)" />
  <xsl:if test="$list">
    <xsl:variable name="value"
      select="substring-before($list, '|')" />
    ...
    <!-- do something with $value -->
    ...
    <!-- recursive call -->
    <xsl:call-template name="parse-one-of">
      <xsl:with-param name="list"
        select="substring-after($list, '|')" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

You need to fill in the middle part of the template to do what you
want it to do with each of the values.

In XSLT 2.0, you can use the tokenize() function to split the value of
one-of into a sequence of strings:

  tokenize(@one-of, '|')

You could then iterate over that sequence with an <xsl:for-each>, for
example. Alternatively, you could parse one-of using the
<xsl:analyze-string> instruction. What's most appropriate depends on
what you want to do with the values.

Of course, if all you want to do is test whether a particular value
appears in the one-of attribute, then you don't need to parse it at
all, just do:

  contains(@one-of, concat('|', $value, '|'))

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/



<Prev in Thread] Current Thread [Next in Thread>