Re: [xsl] Get children and text, excluding a child

Subject: Re: [xsl] Get children and text, excluding a child
From: "Graydon graydon@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 12 Jun 2015 22:40:29 -0000
The identity transform isn't just elements and attributes. (People do
sometimes get annoyed when you strip the comments from their
documents...)

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="number[parent::span]">
    <!-- just in case there are other numbers somewhere else we don't
    want to remove -->
</xsl:template>

will do it.  If you've got a larger transformation, stick a unique mode
on both templates and it will still work so long as you use that mode
when applying templates to the spans.

-- Graydon

On Fri, Jun 12, 2015 at 10:24:43PM -0000, Ryan Graham ryan.graham@xxxxxxxxxx scripsit:
> Apologies for the mangled indentation my mail client imposed. :)
> 
> On 6/12/15, 3:20 PM, "Ryan Graham ryan.graham@xxxxxxxxxx"
> <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> 
> >You could also do an identity transform, omitting number elements with an
> >empty template match:
> >
> ><xsl:template match="/">
> >    <xsl:apply-templates/>
> >	</xsl:template>
> >	
> >
> >	<xsl:template match="@*|*">
> >    <xsl:copy>
> >        <xsl:apply-templates select="@*"/>
> >        <xsl:apply-templates/>
> >    </xsl:copy>
> >	</xsl:template>
> >	
> >
> ><xsl:template match="number"/>
> >
> >
> >Thanks,
> >Ryan
> >
> >On 6/12/15, 1:50 PM, "Rick Quatro rick@xxxxxxxxxxxxxx"
> ><xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> >
> >>I think I figured it out:
> >>
> >><?xml version="1.0" encoding="UTF-8"?>
> >><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> >>    xmlns:xs="http://www.w3.org/2001/XMLSchema";
> >>    exclude-result-prefixes="xs"
> >>    version="2.0">
> >>    
> >>    <xsl:output method="xml" indent="yes"/>
> >>    
> >>    <xsl:template match="/root">
> >>        <root>
> >>            <xsl:for-each select="span">
> >>                <span><xsl:copy-of
> >>select="*[not(self::number)]|text()"/></span>
> >>            </xsl:for-each>
> >>        </root>
> >>    </xsl:template>
> >>    
> >></xsl:stylesheet>
> >>
> >>-----Original Message-----
> >>From: Rick Quatro rick@xxxxxxxxxxxxxx
> >>[mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
> >>Sent: Friday, June 12, 2015 4:45 PM
> >>To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> >>Subject: [xsl] Get children and text, excluding a child
> >>
> >>Hello,
> >>
> >>Here is my input xml:
> >>
> >><?xml version="1.0"?>
> >><root>
> >>  <span><number>9.3</number> <code>protected</code> members</span>
> >>  <span><number>9.4</number> miscellaneous members.</span> </root>
> >>
> >>Here is the desired output:
> >>
> >><?xml version="1.0"?>
> >><root>
> >>  <span> <code>protected</code> members</span>
> >>  <span> miscellaneous members.</span>
> >></root>
> >>
> >>I want to output all of the elements and text of the original <span>
> >>element, but without the <number> element. I am using this for my xsl:
> >>
> >><?xml version="1.0" encoding="UTF-8"?>
> >><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> >>    xmlns:xs="http://www.w3.org/2001/XMLSchema";
> >>    exclude-result-prefixes="xs"
> >>    version="2.0">
> >>    
> >>    <xsl:output method="xml" indent="yes"/>
> >>    
> >>    <xsl:template match="/root">
> >>        <root>
> >>            <xsl:for-each select="span">
> >>                <xsl:copy-of select="descendant::*[not(self::number)]"/>
> >>            </xsl:for-each>
> >>        </root>
> >>    </xsl:template>
> >>    
> >></xsl:stylesheet>
> >>
> >>This is my current (incorrect) output:
> >>
> >><?xml version="1.0" encoding="UTF-8"?>
> >><root>
> >>   <code>protected</code>
> >></root>
> >>
> >>Any help or pointers would be appreciated. Thanks.
> >>
> >>Rick Quatro
> >>Carmen Publishing Inc.
> >>585-366-4017
> >>rick@xxxxxxxxxxxxxxx

Current Thread