5

I'm new to XSL, and not able to find information on this question. This is only for XSLT 1.0, and will eventually be run from XSLTproc.

Here is an example XML

<root>
    <node>
        <data />
        <child>
            <grandchild />
        </child>
        <step-child action="removenode" />
    </node>
    <node>
        <data />
        <step-child action="removenode" />
    </node>
</root>

Basically, I want to keep everything except :

  • remove any node with no <child>
  • remove all <step-child>

I can only figure out how to remove unwanted nodes, but even that is questionable. I really appreciate any help with this.

dur
  • 15,689
  • 25
  • 79
  • 125
J. M. Becker
  • 2,755
  • 30
  • 32
  • I'm trying to shake off the feeling that some professor somewhere suggested to his or her students that they use SO as a way to get their homework done for free. These are definitely not real-world problems, and they're really simple. I'm afraid by doing these folks' homework for them we're depriving them of the process of learning XSLT on their own or via browsing available resources. At a minimum, we should not provide fully functional XSLT programs, but rather just give them the principle, or point them to somewhere that does. –  Jun 02 '12 at 01:43
  • Duplicate of http://stackoverflow.com/questions/321860/how-to-remove-elements-from-xml-using-xslt-with-stylesheet-and-xsltproc and many, many others. –  Jun 02 '12 at 01:48
  • I have a lot of sympathy with torazaburo: I always like to focus on helping the questioner overcome a specific difficulty so that they can solve the problem for themselves, rather than giving them a working answer. It's clear that TechZilla, like many others, is trying to skip a stage in learning a new language: you don't start by trying to solve a specific problem, you start by reading widely about the nature of the language and its general approach to problem solving. – Michael Kay Jun 02 '12 at 17:28
  • @torazaburo: In no way is a duplicate question, elements are not nodes. I actually already could figure that answer with what I've already read. I've read through how XSLT is normally used, to put specific values in templated location. This is not the type of widely documented problem, its not even in any of the official documentation answers. I even know how to remove a specific node, but it's a learning process, which is why I posted the question. That's what this is whole site is for, is it not? – J. M. Becker Jun 02 '12 at 19:35
  • 1
    @MichaelKay: BTW learning from example, has always been the fastest way I learn. I learned BASIC, XHTML, and XML in that very same manner. I also always read the official documentation, and go though some tutorials, but it's often not enough. Its like most XSLT examples are of the same type, which is very different from what I'm doing. – J. M. Becker Jun 02 '12 at 19:40
  • Would have been better if you had shown us what you tried - no matter how bad or broken it was - that way you won't attract homework accusations etc: http://meta.stackexchange.com/a/128553/419 – Kev Jun 04 '12 at 00:53
  • Dear @TechZilla, actually, elements *are* nodes (or to be precise, one type of node). Removing (or not processing) elements/nodes is one of the most basic XSLT design patterns, at its simplest involving nothing more than specifying a null rule (template) that matches the node to be removed. That approach is covered in virtually every online XSLT resource, including in SE many times. More generally, there is an unspoken (?) rule on SE that the poster is expected to have tried to solve the problem and to present his or her solution with the results and why it is not working properly. –  Jun 04 '12 at 01:05

1 Answers1

10
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <!--Identity template to copy all content by default-->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--Remove node elements that do not have child elements, 
               and remove step-child elements -->
    <xsl:template match="node[not(child)] | step-child"/>

</xsl:stylesheet>
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Downvoting to express dissatisfaction the notion of doing simple homework problems for people. –  Jun 02 '12 at 01:49
  • 6
    @torazaburo: If so, then downvote the question -- not the answer -- especially not a good answer. – Dimitre Novatchev Jun 02 '12 at 02:08
  • 2
    @torazaburo: this is not homework, I'm a Linux sysadmin who is somewhat new to XSLT, working with automatic configurations. AND BTW the actual problem is more than even what I asked, this is just the part I had trouble with on my own. – J. M. Becker Jun 02 '12 at 19:42
  • @MadsHansen: This was one of the best, most direct answers I've ever revived from any of the SE sites. I really appreciate your help. – J. M. Becker Jun 02 '12 at 19:45