0
votes

My XML is following:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Rowsets CachedTime="" DateCreated="2014-08-08T05:23:47" EndDate="2014-08-08T05:23:45" StartDate="2014-08-08T04:23:45" Version="14.0.0 Build(802)">
    <Rowset>
        <Row>
            <Name>MAKTX</Name>
            <Value>Ethanol</Value>
            <Min>4</Min>
            <Max>4</Max>
        </Row>
        <Row>
            <Name>SEQNR</Name>
            <Value>T10</Value>
            <Min>5</Min>
            <Max>15</Max>
        </Row>
        <Row>
            <Name>QNT</Name>
            <Value>10</Value>
            <Min>0</Min>
            <Max>10000000000000</Max>
        </Row>
    </Rowset>
    <Rowset>
        <Row>
            <Name>MAKTX</Name>
            <Value>CO2</Value>
            <Min>4</Min>
            <Max>4</Max>
        </Row>
        <Row>
            <Name>SEQNR</Name>
            <Value>T8</Value>
            <Min>5</Min>
            <Max>15</Max>
        </Row>
        <Row>
            <Name>QNT</Name>
            <Value/>
            <Min>0</Min>
            <Max>10000000000000</Max>
        </Row>
    </Rowset>
    <Rowset>
        <Row>
            <Name>MAKTX</Name>
            <Value/>
            <Min>4</Min>
            <Max>4</Max>
        </Row>
        <Row>
            <Name>SEQNR</Name>
            <Value>-- Select One --</Value>
            <Min>5</Min>
            <Max>15</Max>
        </Row>
        <Row>
            <Name>QNT</Name>
            <Value/>
            <Min>0</Min>
            <Max>10000000000000</Max>
        </Row>
    </Rowset>
</Rowsets>

I want a resultant XML which contains only those <Rowset> where [Name='QNT']/Value = '' and [Name = 'MAKTX']/Value = ''.

My XSLT looks like following:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="http://xml.apache.org/xslt/java" exclude-result-prefixes="java" version="1.0">
    <xsl:output media-type="text/xml" method="xml"/>
    <!-- Filters refdoc based on condition and  data  -->
    <xsl:template match="/Rowsets">
        <Rowsets>
            <xsl:for-each select="/Rowsets/Rowset">
                <xsl:choose>
                    <xsl:when test="Rowset[Row[Name='QNT']/Value = '' and Row[Name = 'MAKTX']/Value = '']">
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="."/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </Rowsets>
    </xsl:template>
</xsl:stylesheet>

But it is giving me entire XML as output and not removing the last <Rowset>. I tried variosa permutations and combinations but no success.

What am I doing wrong here?

1

1 Answers

2
votes

The problem is one of context. You are iterating over Rowset elements, like so

 <xsl:for-each select="/Rowsets/Rowset">

This means within the xsl:for-each you are positioned on a Rowset element, and so any xpath expression will be relative to that. Thus, when you do <xsl:when test="Rowset[... it is looking for Rowset that is a child of the current Rowset.

What you need to do is just this....

<xsl:when test="Row[Name='QNT']/Value = '' and Row[Name = 'MAKTX']/Value = ''">

In fact, you don't need an xsl:choose here at all. You can make the expression part of the xsl:for-each

  <xsl:for-each select="Rowset[not(Row[Name='QNT']/Value = '' and Row[Name = 'MAKTX']/Value = '')]">
      <xsl:copy-of select="."/>
 </xsl:for-each>

Having said that, you don't even need the xsl:for-each. You could just do this...

<xsl:copy-of select="Rowset[not(Row[Name='QNT']/Value = '' and Row[Name = 'MAKTX']/Value = '')]" />