473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Remove sibling elements according to the value of an attribute

Hi,

how can remove sibling elements based on the value of an attribute ?

For instance, gven the XML document:

<root>
<parentElment>
<testElement name="A">
<removableElement/>
</parentElment>
<parentElment>
<testElement name="B">
<removableElement/>
</parentElment>
</root>

I would write a XSLT transformer that implements the following rule:

IF testElement[@name='A'] THEN <remove sibling element
removableElement>

so that the resultng XML document looks lke the following:

<root>
<parentElment>
<testElement name="A">
</parentElment>
<parentElment>
<testElement name="B">
<removableElement/>
</parentElment>
</root>

Thanks a lot for any suggestion

Aug 22 '06 #1
3 2724
IF testElement[@name='A'] THEN <remove sibling element
removableElement>
Reverse the logic: when processing removableElements, copy them only if
there is not a testElement[@name='A']

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Aug 22 '06 #2
pa****************@googlemail.com wrote:
Hi,

how can remove sibling elements based on the value of an attribute ?

For instance, gven the XML document:

<root>
<parentElment>
<testElement name="A">
<removableElement/>
</parentElment>
<parentElment>
<testElement name="B">
<removableElement/>
</parentElment>
</root>

I would write a XSLT transformer that implements the following rule:

IF testElement[@name='A'] THEN <remove sibling element
removableElement>

so that the resultng XML document looks lke the following:

<root>
<parentElment>
<testElement name="A">
</parentElment>
<parentElment>
<testElement name="B">
<removableElement/>
</parentElment>
</root>
Think of it the other way round: if the attribute value is not A,
then process the following sibling:

<xsl:template match="testElement">
<testElement>
<xsl:if test="@name!='A' and following-sibling::removableElement">
<removableElement/>
</xsl:if>
</testElement>
</xsl:template>

///Peter
Aug 22 '06 #3
Thanks a lot for your suggestions; applying them I was able to obtain
what I wanted, but I'm wandering if there is a better approach to get
the same result...

Given the source XML document:

<?xml version="1.0" encoding="UTF-8"?>
<sample xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNameSpaceSchemaLocation="sample.xsd">
<parent-element>
<child-element name="AAA" value="XXX"/>
<sibling-element-1 id="001">Sibling 1</sibling-element-1>
<sibling-element-2 id="002">Sibling 2</sibling-element-2>
</parent-element>
<parent-element>
<child-element name="BBB" value="XXX"/>
<sibling-element-1 id="010">Sibling 1>/sibling-element-1>
<sibling-element-2 id="020">Sibling 2</sibling-element-2>
</parent-element>
<parent-element>
<child-element name="CCC" value="XXX"/>
<sibling-element-1 id="100">Sibling 1</sibling-element-1>
<sibling-element-2 id="200">Sibling 2</sibling-element-2>
</parent-element>
</sample>

I can obtain the following desired result:

<?xml version="1.0" encoding="UTF-16"?>
<sample xsi:noNameSpaceSchemaLocation="sample.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<parent-element>
<child-element name="AAA" value="aNewValue1"></child-element>
<sibling-element-1 id="001">Sibling 1</sibling-element-1>
<sibling-element-2 id="002">Sibling 2</sibling-element-2>
</parent-element>
<parent-element>
<child-element name="BBB" value="aNewValue2"></child-element>
<sibling-element-1 id="010">Sibling 1</sibling-element-1>
<sibling-element-2 id="020">Sibling 2</sibling-element-2>
</parent-element>
<parent-element>
<child-element name="CCC" value="aNewValue3"></child-element>
</parent-element>
</sample>

using the following XSLT transformer:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="child-element[@name = 'AAA']/@value">
<xsl:attribute name="{name()}">aNewValue1</xsl:attribute>
</xsl:template>
<xsl:template match="child-element[@name = 'BBB']/@value">
<xsl:attribute name="{name()}">aNewValue2</xsl:attribute>
</xsl:template>
<xsl:template match="child-element[@name = 'CCC']/@value">
<xsl:attribute name="{name()}">aNewValue3</xsl:attribute>
</xsl:template>
<xsl:template match="parent-element/sibling-element-1">
<xsl:if test="preceding-sibling::child-element[@name != 'CCC']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
<xsl:template match="parent-element/sibling-element-2">
<xsl:if test="preceding-sibling::child-element[@name != 'CCC']">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

Do you think that this is a valid approach or maybe there is a better
one, especially for the removal of elements according to the value of
the name attribute of the sibling child-element ?

Thanks again for your support.

Regards,

Patrizio

Aug 23 '06 #4

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

Similar topics

1
by: Johannes Koch | last post by:
How to remove multiple elements with the same child element content? E.g. input: <root> <foo> <bar>ABC</bar> </foo> <foo> <bar>DEF</bar> </foo>
3
by: uNConVeNtiOnAL | last post by:
Why doesn't this work - no errors, just no value when text is in textbox var fn=document.forms.elements.value; Thanks Tom
2
by: redog6 | last post by:
I would be grateful if someone can tell me how to specify sibling elements in any order - I am using XMLSPY. Many thanks Redge
2
by: charles-brewster | last post by:
I'm trying to write a simple JavaScript function which will use a button to copy table cell data into a form input text box as the "value" attribute. The following is intended to test the...
4
by: Matt Kruse | last post by:
According to standards, if an option is selected and it has no VALUE attribute, the contents of the option tag is to be submitted. So: <select name="sel"> <option selected>Value</option>...
6
by: Lisa | last post by:
I am reading in data from a text file. I want to enter each value on the line into a list and retain the order of the elements. The number of elements and spacing between them varies, but a...
1
by: Nathan Sokalski | last post by:
I have a page that I created with ASP.NET which contains a RadioButtonList. This obviously shows up as input tags in the generated page. When I was looking at the page with the free Internet...
0
by: Nathan Sokalski | last post by:
In the RadioButton and CheckBox controls, there is no Value attribute. The html <inputtag has a value attribute, which is used by both type="radio" and type="checkbox". The RadioButtonList and...
3
Jezternz
by: Jezternz | last post by:
Ok, I want to remove an element from the standard flow, so its location is not affected or determined by other sibling elements, the only catch is, I dont want to position it using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.