Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 17th, 2008, 10:59 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default Test for empty nodes, including only whitespace

I am using Saxon9 on Windows

Here's my problem


I am given an XML document that is formatted such as:

Code:
<A>
  <B>
    <C>xyzzy</C>
  <D>
  ...
  </D>
  </B>
  <B>
    <C> </C>
  <D>
   ...
   </D>
   </B>
  <B>
    <C></C>
  <D>
   ...
   </D>
   </B>
  <B>
    <C>abc 123</C>
  </B>
</A>
My job is to translate this to another XML format that is basically the same structure, but different tags. No problem.

Later they come back a want me to only translate the 'C' tags if there is meaningful data in them. So the blank one and the one with only a whitespace need to be left out of the result.

So the result would look like:
Code:
<A>
  <B>
    <C>xyzzy</C>
  <D>
   ...
   </D>
   </B>
  <B>
<!-- dropped the C with whitespace content -->
  <D>
    ...
    </D>
    </B>
   <B>
<!-- dropped the C with empty content -->
  <D>
    ...
    </D>
    </B>
   <B>
     <C>abc 123</C>
  </B>
</A>
I've tried various analyze-string regex's, but they are not giving me the result I'm after.

<xsl:analyze-string select="string()" regex="^[\S*]$">

seemed like a good place to start, if there is a non-whitespace character anywhere in the string, then copy it to the result.

Does anyone have any suggestions?

Thanks!
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare
 
Old December 17th, 2008, 11:24 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You just need two template rules like this:

<xsl:template match="C[normalize-space(.)]">
<xsl:copy-of select="."/>
</xsl:template>

<xsl:template match="C"/>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 17th, 2008, 03:40 PM
Friend of Wrox
 
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

Thanks!

I believe this also solves the other problem I had with ignoring leading and trailing whitespace during a comparison.

What I ended up doing for the problem at hand ended up looking like this
Code:
    <xsl:template match="C[normalize-space(.)]" mode="rqC">
        <xsl:if test="string() != ''">
            <Cmod>
                <xsl:apply-templates/>
            </Cmod>
        </xsl:if>
    </xsl:template>

<!--
==================
-->
    <xsl:template match="C"/>
I found if I didn't eat the 'C' node alone, I would get some undesired content in the result.
__________________
------------------------
Keep Moving Forward

GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876

Michael Hare

Last edited by mphare; December 17th, 2008 at 03:43 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
xsl:test, but ignoring whitespace mphare XSLT 1 December 5th, 2008 05:23 AM
test for empty content? rakesh XSLT 2 July 29th, 2008 05:51 PM
Counting Nodes That Are Not Empty tommyready XSLT 2 September 4th, 2007 09:03 AM
How do I test for an empty array element kmoran Excel VBA 1 October 8th, 2004 03:34 AM
whitespace in label Twinklestar ASP.NET 1.0 and 1.1 Basics 4 July 28th, 2004 07:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.