Empty nodes vs no nodes

Questions about XML that are not covered by the other forums should go here.
Brian_donovan
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="">
    <title></title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>19.90</price>
    <year>1928</year>
  </cd>
  
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:choose>
 <xsl:when test=".='' "><text style="color:red;"> Error </text></xsl:when>
   <xsl:otherwise>
         <xsl:value-of select="."/>
 </xsl:otherwise>
</xsl:choose>
</xsl:for-each>
'

adrian
Posts: 2573
Joined: Tue May 17, 2005 4:01 pm

Re: Empty nodes vs no nodes

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

Hi,

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
REgards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com

Post Reply