0

I am trying to filter an xml file in a .Net application I am developing. Some sample xml below, obviously not the proper xml, but near enough :-)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row A1="1" A2="AMS">
    <Name>Ashley</Name>
    <Team>Team B</Team>
    <Date>3/25/2012</Date>
    <Value>511681.15</Value>
</Row>
<Row A1="2" A2="AMS">
    <Name>Kylie</Name>
    <Team>Team A</Team>
    <Date>9/28/2010</Date>
    <Value>408438.47</Value>
</Row>
<Row A1="3" A2="AMS">
    <Name>Gianna</Name>
    <Team>Team B</Team>
    <Date>40004</Date>
    <Value>109709.22</Value>
</Row>
<Row A1="4" A2="AMS">
    <Name>Chase</Name>
    <Team>Team F</Team>
    <Date>40152</Date>
    <Value>279018.79</Value>
</Row>

The stylesheet has a param that is set by XsltArgumentList in the application. The param is passed into the stylesheet, but does not filter the xml. I have tried using the ms node-set and exsl node-set but only get the top level root returned. Stylesheet below:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:decimal-format name="NN" NaN="0" />
<xsl:param name="Filter"  />
<xsl:template match="/">
    <Root>
        <xsl:for-each select="exsl:node-set($Filter)">
            <Row>
                <xsl:attribute name="A1">
                    <xsl:value-of select="@A1" />
                </xsl:attribute>
                <xsl:attribute name="A2">
                    <xsl:value-of select="@A2" />
                </xsl:attribute>
                <Team>
                    <xsl:value-of select="Team"/>
                </Team>
            </Row>
        </xsl:for-each>
    </Root>
</xsl:template>

The filter i am trying to pass could be using any of the combination of the xml elements. The filter is am currently attempting is below

<xsl:param name="Filter" select="//Row[Team='Team A']" />

But this is just returns

<?xml version="1.0" encoding="utf-8"?>
<Root>
<Row A1="" A2="">
<Team></Team>
</Row>
</Root>

Any help or pointers would be appreciated!

Thanks

GrahamCFNewc
  • 135
  • 6

1 Answers1

0

This will not woke like you tried it, because you can't have a xpath expression in an XLST parameter or variable. You can use variable or parameter in please of const values.

Therefore you can use something like

<xsl:param name="teamFilter" select="'Team A'" />
...
 <xsl:for-each select="//Row[Team='$teamFilter']">

Or if you have to filter for different nodes you may try:

<xsl:param name="filterValue" select="'Team A'" />
<xsl:param name="filterNode" select="'Team'" />

...

<xsl:for-each select="//Row[*[name()= $filterNode and . =$filterValue]]">

Other alternatives would be:

  • Patch the XSLT before loading it
  • Use xpath iterator with XPathExpression from .NET.
hr_117
  • 9,589
  • 1
  • 18
  • 23