• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Remove sibling elements according to the value of the attribute of another element

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

given the following 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 of child-element AAA</sibling-element-1>
<sibling-element-2 id="002">Sibling 2 of child-element AAA</sibling-element-2>
</parent-element>
<parent-element>
<child-element name="BBB" value="XXX"/>
<sibling-element-1 id="003">Sibling 1 of child-element BBB</sibling-element-1>
<sibling-element-2 id="004">Sibling 2 of child-element BBB</sibling-element-2>
</parent-element>
<parent-element>
<child-element name="CCC" value="XXX"/>
<sibling-element-1 id="005">Sibling 1 of child-element CCC</sibling-element-1>
<sibling-element-2 id="006">Sibling 2 of child-element CCC</sibling-element-2>
</parent-element>
</sample>

I would otain the following 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>

I'm able to get it 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 a lot for any suggestion.

Patrizio
 
Sheriff
Posts: 28331
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you had asked how to remove elements from an XML document, I would have suggested using an XSLT identity transformation plus a template that failed to copy the elements you wanted to remove. And that's what you have there. So yes, I approve.

Besides, it works, right?
 
Don't listen to Steve. Just read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic