Subject: RE: [xsl] How to copy all child elements except specific one? From: "Morris, Chris" <ChrisM@xxxxxxxxxxxxxxxx> Date: Tue, 16 Jan 2001 13:49:55 -0600 |
> <xsl:copy-of select="*[not(self::thisElement)]"/> > > does what you ask, but this method only works if you don't mind > changing the order of your child nodes. This is not quite what I need. I haven't given the full picture because I was hoping I could pinpoint the question and apply it to my case. But here's the full story. I'm starting with this xml: <?xml version="1.0"?> <Level1> <Level2 Name="ALevel2Element"> <Level3 L3Attr="L3AttrValue"> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <ChildElementToChange OldValue="A" NewValue="D" /> </Level3> </Level2> </Level1> I want to get here: <?xml version="1.0"?> <Level1> <Level2 Name="ALevel2Element"> <Level3> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <ChildElementToChange> <NewElement L3Attr="L3AttrValue" OldValue="A" NewValue="D" /> </ChildElementToChange> </Level3> </Level2> </Level1> The tricky bit is moving the Level3 attribute into the modified ChildElementToChange fragment. I'm close at the moment -- I've got this XSL: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="Level3"> <xsl:copy-of select="*[not(self::thisElement)]" /> <!-- *** magic line --> <xsl:variable name="L3Attr" select="@L3Attr" /> <xsl:for-each select="ChildElementToChange"> <xsl:call-template name="ChangeChildElement"> <xsl:with-param name="L3Attr"><xsl:value-of select="$L3Attr" /></xsl:with-param> </xsl:call-template> </xsl:for-each> </xsl:template> <xsl:template name="ChangeChildElement"> <xsl:param name="L3Attr" /> <xsl:element name="ChildElementToChange"> <xsl:element name="NewElement"> <xsl:attribute name="L3Attr"><xsl:value-of select="$L3Attr" /></xsl:attribute> <xsl:attribute name="OldValue"><xsl:value-of select="@OldValue" /></xsl:attribute> <xsl:attribute name="NewValue"><xsl:value-of select="@NewValue" /></xsl:attribute> </xsl:element> </xsl:element> </xsl:template> </xsl:stylesheet> But this copy-of expression you gave me eliminates the Level3 node from being copied into the output (though all of its child nodes go down). I need the copy-of to copy self (Level3), plus all children EXCEPT ChildElementToChange because it will be modified via the template call. So if I change the copy-of select to <xsl:copy-of select="." /> I get this: <?xml version="1.0" encoding="UTF-16" ?> <Level1> <Level2 Name="ALevel2Element"> <Level3 L3Attr="L3AttrValue"> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <TestElementA Attr="Value" /> <ChildElementToChange OldValue="A" NewValue="D" /> </Level3> <ChildElementToChange> <NewElement L3Attr="L3AttrValue" OldValue="A" NewValue="D" /> </ChildElementToChange> </Level2> </Level1> ... which is close except: - the original ChildElementToChange is retained - the new ChildElementToChange is outside the Level3 element when it should be inside - the Level3 L3Attr is retained when it should be eliminated Any ideas? Chris XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] How to copy all child ele, Dimitre Novatchev | Thread | Re: [xsl] How to copy all child ele, David Carlisle |
Re: [xsl] How to copy all child ele, Dimitre Novatchev | Date | RE: [xsl] How to copy all child ele, Morris, Chris |
Month |