Empty nodes vs no nodes

Questions about XML that are not covered by the other forums should go here.
Posts: 3
Joined: Tue Sep 17, 2019 10:27 pm

Empty nodes vs no nodes

Post by Brian_donovan » Mon Sep 30, 2019 3:09 am

Hi, pleas help! I am creating a XSLT 1.0 formato to export data from an XML doc. I have an example of my problem below:

Code: Select all

 <cd name="Bonny" size="">
    <artist>Bonnie Tyler</artist>
    <company>CBS Records</company>
I am extracting all the data using only wildcards like name(), /* or . so I do not know the name of any node this is just an example.

My problem is that when getting the values the node <title> returns nothing I need to get an error text or something worning me that there is a node with nothing. My test code lookes like this but it cant see the empty tag pleas help

Code: Select all

<xsl:for-each select="*//@*">
 <xsl:when test=".='' "><text style="color:red;"> Error </text></xsl:when>
         <xsl:value-of select="."/>

Posts: 2655
Joined: Tue May 17, 2005 4:01 pm

Re: Empty nodes vs no nodes

Post by adrian » Tue Oct 01, 2019 3:51 pm


There is no empty text node. The node just doesn't exist.
Also, <xsl:for-each select="*//@*"> iterates only on attributes, everything else (elements and text) is ignored.
If you want to iterate on elements, maybe use <xsl:for-each select="//*"> Although I should mention there are better ways to do this in XSLT, by using xsl:templates.
Then test if there is no text() node:

Code: Select all

<xsl:when test="not(text())"><text style="color:red;"> Error in <xsl:value-of select="name(.)"/></text></xsl:when
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger

Post Reply