Solved

XPath to select nodes where child node is absent or empty?

Posted on 2002-03-14
3
346 Views
Last Modified: 2012-08-13
In the following XML, I would like to select all employees without a middle name either because the node is empty or because the node is absent. In this example, the result would be Bill Gates and Larry Elison.

<Employees>
  <Employee>
    <FirstName>Bill</FirstName>
    <MiddleName></MiddleName>
    <LastName>Gates</LastName>
  </Employee>
  <Employee>
    <FirstName>George</FirstName>
    <MiddleName>W</MiddleName>
    <LastName>Bush</LastName>
  </Employee>
  <Employee>
    <FirstName>Larry</FirstName>
    <LastName>Elison</LastName>
  </Employee>
0
Comment
Question by:FrenchJericho
  • 3
3 Comments
 
LVL 23

Expert Comment

by:b1xml2
ID: 6866705
XPath statement
===============
//Employee[string-length(normalize-space(MiddleName)) != 0]
0
 
LVL 23

Accepted Solution

by:
b1xml2 earned 50 total points
ID: 6867661
oops misread the question, the XPath is
//Employee[string-length(normalize-space(MiddleName)) = 0 or not(MiddleName)]
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6867694
in fact, the following would be sufficient
//Employee[string-length(normalize-space(MiddleName)) = 0]
XML Document
============
<?xml version="1.0" encoding="iso-8859-1"?>
<Employees>
 <Employee>
   <FirstName>Bill</FirstName>
   <MiddleName></MiddleName>
   <LastName>Gates</LastName>
 </Employee>
 <Employee>
   <FirstName>George</FirstName>
   <MiddleName>W</MiddleName>
   <LastName>Bush</LastName>
 </Employee>
 <Employee>
   <FirstName>Larry</FirstName>
   <LastName>Elison</LastName>
 </Employee>
</Employees>

XSLT Document
-------------
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0"
 exclude-result-prefixes="msxsl">
<xsl:output method="xml" encoding="iso-8859-1" indent="yes" />
<xsl:template match="/">
<Employees>
<xsl:copy-of select="//Employee[string-length(normalize-space(MiddleName)) = 0]" />
</Employees>
</xsl:template>
</xsl:stylesheet>

Output
========
<?xml version="1.0" encoding="iso-8859-1"?>
<Employees>
<Employee>
<FirstName>Bill</FirstName>
<MiddleName></MiddleName>
<LastName>Gates</LastName>
</Employee>
<Employee>
<FirstName>Larry</FirstName>
<LastName>Elison</LastName>
</Employee>
</Employees>
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question