473,653 Members | 2,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

removal of empty nodes resulting from application of other templates

I'm trying to write a stylesheet which removes nodes which are empty
as a result of other template processing.

For example, given the following xml, I'd like to:
- remove all "b" elements
- remove all "a" elements which, as a result of "b" element
removal, now have no children

so, starting with:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>
<b/>
</a>
<a>
<b/>
<c/>
</a>
</doc>

I'd like to end up with:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
<a>
<c/>
</a>
</doc>

I've written this stylesheet:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="ye s" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- matches all nodes and attributes -->
<xsl:template match="node()|@ *" name="identity" >
<xsl:copy>
<xsl:apply-templates select="node()| @*"/>
</xsl:copy>
</xsl:template>

<!-- rule for element "a", ought to reproduce "a" only if it has
children -->
<xsl:template match="//a">
<xsl:if test="count(./*) &gt; 0">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>

<!-- remove all b nodes -->
<xsl:template match="//b" priority="3" name="b-removal"/>

</xsl:stylesheet>

but the resultant output has not removed the empty "a" element:
<doc>
<a/>
<a>
<c/>
</a>
</doc>

Can anyone suggest a way to do this?

Thanks,

Ted

Feb 13 '07 #1
3 2051
On 13 Feb., 23:57, tschwa...@revas ystems.com wrote:
I'm trying to write a stylesheet which removes nodes which are empty
as a result of other template processing.
How about applying that stylesheet to your data twice?

Regards,
Johannes
Feb 14 '07 #2
jo************* @gmx.net wrote:
>>I'm trying to write a stylesheet which removes nodes which are empty
as a result of other template processing.
XSLT 1.0 doesn't have any built-in ability to do two-pass processing.
The usual solution is to run two stylesheets in succession, or the same
one twice.

If you MUST do it in a single stylesheet, the usual workaround is to use
the EXSLT node-set function. Do a first pass outputting into a variable
to obtain a Result Tree Fragment, then use exslt:node-set() to make that
available to XSLT in a form that it can walk over again, generating the
final output. Modes may be useful in keeping the two passes separate, if
the behavior is different in one than in the other.

(XSLT 2.0 -- still not widely supported, unfortunately, since it adds a
lot of complexity along with the simplifications -- eliminates the
distinction between node-sets and RTFs, and can do what I've just
described without needing a "standard nonstandard" helper.)

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Feb 14 '07 #3
On Feb 13, 9:32 pm, Joseph Kesselman <keshlam-nos...@comcast. net>
wrote:
johanneskrue... @gmx.net wrote:
>I'm trying to write a stylesheet which removes nodes which are empty
as a result of other template processing.

XSLT 1.0 doesn't have any built-in ability to do two-pass processing.
The usual solution is to run two stylesheets in succession, or the same
one twice.

If you MUST do it in a single stylesheet, the usual workaround is to use
the EXSLT node-set function. Do a first pass outputting into a variable
to obtain a Result Tree Fragment, then use exslt:node-set() to make that
available to XSLT in a form that it can walk over again, generating the
final output. Modes may be useful in keeping the two passes separate, if
the behavior is different in one than in the other.

(XSLT 2.0 -- still not widely supported, unfortunately, since it adds a
lot of complexity along with the simplifications -- eliminates the
distinction between node-sets and RTFs, and can do what I've just
described without needing a "standard nonstandard" helper.)

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Thanks for the help! As I would like to apply the templates in one
pass I've successfully used the approach of using included
stylesheets and modes described above. Here are the stylesheets:
first-pass.xsl:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:exslt="ht tp://exslt.org/common">
<xsl:output omit-xml-declaration="ye s" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- matches all nodes and attributes -->
<xsl:template match="node()|@ *" name="identity" mode="first-pass">
<xsl:copy>
<xsl:apply-templates select="node()| @*" mode="first-pass"/>
</xsl:copy>
</xsl:template>

<xsl:template match="/">
<xsl:apply-templates mode="first-pass"/>
</xsl:template>

<!-- remove all b nodes -->
<xsl:template match="//b" priority="3" name="b-removal" mode="first-
pass"/>

</xsl:stylesheet>

second-pass.xsl:

<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:exslt="ht tp://exslt.org/common">
<xsl:output omit-xml-declaration="ye s" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- matches all nodes and attributes -->
<xsl:template match="node()|@ *" name="identity" mode="second-pass">
<xsl:copy>
<xsl:apply-templates select="node()| @*" mode="second-pass"/>
</xsl:copy>
</xsl:template>

<xsl:template match="/">
<xsl:apply-templates mode="second-pass"/>
</xsl:template>

<!-- rule for element "a", ought to reproduce a only if it has
children -->
<xsl:template match="//a" mode="second-pass">
<xsl:if test="count(./*) &gt; 0">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>

</xsl:stylesheet>

main.xsl:
<xsl:styleshe et version="1.0"
xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
xmlns:exslt="ht tp://exslt.org/common">
<xsl:output omit-xml-declaration="ye s" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:include href="first-pass.xsl"/>
<xsl:include href="second-pass.xsl"/>

<xsl:template match="/">
<xsl:variable name="first">
<xsl:apply-templates mode="first-pass" />
</xsl:variable>
<xsl:apply-templates select="exslt:n ode-set($first)" mode="second-
pass" />
</xsl:template>

</xsl:stylesheet>
Then with execution of main.xsl against the source xml, I get the
desired output:

<doc>
<a>
<c/>
</a>
</doc>

Thanks again.

Feb 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1964
by: Erhard Schwenk | last post by:
Hi there, Maybe this is a faq, if so, point me the URL please. I have to do some xml to xml transformations in xslt, where I have - simplified - the following: input: <doc> <a/><a/><b/><a><a><a><b/><a><b/><a><a>
1
2745
by: Johannes Lebek | last post by:
Hi there, somehow, I cannot access nodes that are stored in a variable. I'm using Xalan 2.5.1 and the following commands: ================ BEGIN ==================== <xsl:variable name="referenced-node" select="//a"/> <xsl:variable name="attribute-source">
4
4037
by: Jyrki Keisala | last post by:
Hi, I have an XML file which I want to present as a HTML table, and I'm looking for a quick way of defaulting all empty elements in my XML file to a nbsp (using the   notation) in my XSL transformation file, so that if I have for example an XML record of <record> <elem1>fii</elem1> <elem2 />
4
1608
by: dd | last post by:
Given the bolow sample, I need to count the elements <quick> with a child 'yes'. The result for this sample should, of couse, be '1', but I cannot figure out how to write the expression in the <xsl:template select="????"> I tried count(//quick/*) >0 which returns false... <?xml version="1.0" encoding="UTF-8"?> <dffob>
3
7618
by: Michael | last post by:
Hello, I am creating an XSL that is going to create a report from XML we recieve from another system. The XML would look like: <report> <page> <header1> <data1>asdf</data1> <data2>fdas</data2>
2
1714
by: | last post by:
Okay, this is probably a really stupid question - but I just can't figure it out. I have a large xml document which is being transformed into another using ..net xsl - some elements inside of which do not have any data at some times but do at others. If they don't have any data, I want to suppress the generation of the tag completely in the resulting xml. So for example, if I have a document:
1
4281
by: tMan | last post by:
whats the xpath to get all the empty nodes in a document. in the xml below i want to get the nodes <empty> and <t> all other nodes either have a child node or text in them <root> <test> <se> <li>test</li> </se> </test> <bs>
4
4737
by: Michael Frost | last post by:
Loading and saving a xml document in .net creates whitespace in empty nodes! loading an empty node like: <node></node> ..net saves as: <node> </node>
4
9013
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
0
8370
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8283
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8590
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.