?
Solved

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

Posted on 2002-03-14
3
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

752 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