xsl-list
[Top] [All Lists]

Re: [xsl] filter using contains with multiple values

2016-03-04 05:38:58
Raimund Kammering raimund(_dot_)kammering(_at_)desy(_dot_)de wrote:
okay got that point (parenthesis). But still that does not work for me since 
somehow the string in filter_value is not understood as sequence!
So if I now use:

  <xsl:if test="*[name()= $filter and . = $filter_value]”>

or as Michael suggested

<xsl:if test="*[name()= $filter][. = $filter_value]”>

it only matches the first of the two in the sequence if I use

<xsl:param name="filter_value" as="xs:string*" select="'Log', ‘Info'”/>

to pass the parameter! Even an:

<xsl:value-of select=“$filter_value”/>

only returns: ‘Log’ where I would expect it to return the whole parameter! I 
seem to still miss some crucial point in making filter_value being understood as sequence 
here!?

Are you sure you are using an XSLT 2.0 processor with the
  <xsl:stylesheet version="2.0"
? The xsl:value-of results sounds as if you have the wrong version attribute on your xsl:stylesheet or xsl:transform (or somewhere in your code you set xsl:version to 1.0).

For me

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs">

<xsl:param name="filter" as="xs:string" select="'keyword'"/>
<xsl:param name="filter_values" as="xs:string*" select="'Log', 'Info'"/>

<xsl:template match="/">
    <xsl:copy-of select="//*[name() = $filter and . = $filter_values]"/>
</xsl:template>


</xsl:transform>

when run against

<list>
  <entry>
    <keyword>Log</keyword>
    <location>A</location>
  </entry>
  <entry>
    <keyword>Log</keyword>
    <location>B</location>
  </entry>
  <entry>
    <keyword>Problem</keyword>
    <location>A</location>
  </entry>
  <entry>
    <keyword>Info</keyword>
    <location>B</location>
  </entry>
</list>

with Saxon 9 (see online at http://xsltransform.net/gWvjQg1) returns the three elements

<keyword>Log</keyword><keyword>Log</keyword><keyword>Info</keyword>


The contains check with "some" would be

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"; exclude-result-prefixes="xs">

<xsl:param name="filter" as="xs:string" select="'keyword'"/>
<xsl:param name="filter_values" as="xs:string*" select="'Log', 'Info'"/>

<xsl:template match="/">
<xsl:copy-of select="//*[name() = $filter and (some $value in $filter_values satisfies contains(., $value))]"/>
</xsl:template>


</xsl:transform>

with the sample

<list>
  <entry>
    <keyword>Log</keyword>
    <location>A</location>
  </entry>
  <entry>
    <keyword>Log</keyword>
    <location>B</location>
  </entry>
  <entry>
    <keyword>Problem</keyword>
    <location>A</location>
  </entry>
  <entry>
    <keyword>The newest Info</keyword>
    <location>B</location>
  </entry>
</list>


giving

<keyword>Log</keyword><keyword>Log</keyword><keyword>The newest Info</keyword>

(Online at http://xsltransform.net/gWvjQg1/1)
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

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