11

I would like to select only those node where child node value matches a certain value.

Here is my orig XML:

This is my orig XML

<Entry>
 <Name>AAA</Name>
 <line id="1">A</line>
 <line id="2">B</line>
</Entry>
<Entry>
 <Name>BBB</Name>
 <line id="1">C</line>
 <line id="2">D</line>
</Entry>
<Entry>
 <Name>AAA</Name>
 <line id="1">E</line>
 <line id="2">F</line>
</Entry>
<Entry>
 <Name>CCC</Name>
 <line id="1">G</line>
 <line id="2">H</line>
</Entry>

I would like to extract all entries where Name = 'AAA', so the result would be:

<Entry>
 <Name>AAA</Name>
 <line id="1">A</line>
 <line id="2">B</line>
</Entry>
<Entry>
 <Name>AAA</Name>
 <line id="1">E</line>
 <line id="2">F</line>
</Entry>

I am limited to using XSLT 1.0.

Please provide any guidance. I am stuck on how to drop all sub-nodes for others that do not match.

regards, Rahul

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Rahul
  • 137
  • 1
  • 1
  • 5

3 Answers3

17

The following will select all entry nodes with subnodes 'Name' that equal AAA.

//Entry[Name = "AAA"]
xshoppyx
  • 1,444
  • 1
  • 8
  • 9
4

Try something like this (List element added to get well-formed xml):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <List>
      <xsl:apply-templates select="//Entry[Name='AAA']"/>
    </List>
  </xsl:template>

  <xsl:template match="Entry">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>
jpj
  • 49
  • 2
3

How about

//Name[text()='AAA']/..

find all Name nodes whose text content is AAA, then move up one level to Name's parent node, which'd be Entry.

Marc B
  • 356,200
  • 43
  • 426
  • 500