Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Re: Clone a Nodeset and then modify some Element >Thread Next - Re: Clone a Nodeset and then modify some Element Re: Clone a Nodeset and then modify some ElementTo: NULL Date: 9/3/2008 7:40:00 AM In the xsl-list, to which you cross-posted this identical message there already were good replies -- just read about the "identity rule" in XSLT. This is definitely the most important XSLT design pattern. Cheers, Dimitre Novatchev "Martin Honnen" <mahotrash@y...> wrote in message news:uUHEW$bDJHA.3392@T...... > bigAPE wrote: >> Hi All, >> >> I'm trying to clone a nodeset and alter one or two of the element >> values in an attempt to save me having to rebuild all of them or copy >> them all over and add the new ones. >> >> So What I would like to do is "Copy" SomeBlock named "A" with all the >> 50+ child elements intact, but modify one or more random fields with >> updated values >> >> ----------------------------- >> SAMPLE INPUT >> ----------------------------- >> >> <wrapper> >> <SomeBlock name="A"> >> <SomeField name="A1">SomeValue1</SomeField> >> <SomeField name="A2">SomeValue2</SomeField> >> <SomeField name="A3">SomeValueA</SomeField> >> <SomeField name="A4">SomeValueB</SomeField> >> ...and about 50 more elements >> </SomeBlock> >> <SomeBlock name="B"> >> <SomeField name="A1">SomeValue2</SomeField> >> <SomeField name="A2">SomeValue1</SomeField> >> <SomeField name="A3">SomeValueF</SomeField> >> <SomeField name="A4">SomeValueE</SomeField> >> ...and about 50 more elements >> </SomeBlock> >> <SomeBlock name="C"> >> <SomeField name="A1">SomeValue2</SomeField> >> <SomeField name="A2">SomeValue1</SomeField> >> <SomeField name="A3">SomeValueF</SomeField> >> <SomeField name="A4">SomeValueE</SomeField> >> ...and about 50 more elements >> </SomeBlock> >> ...more SomeBlocks >> <actions> >> <action type="Update" ref="A"> >> <field name="A2">SomeValue9</Field> >> <field name="A4">SomeValueX</Field> >> </action> >> <action type="Update" ref="C"> >> <field name="A1">SomeValue5</Field> >> </action> >> </actions> >> </wrapper> >> >> ----------------------------- >> SAMPLE OUTPUT >> ----------------------------- >> The output of the XSLT would be as follows: >> >> <Results> >> <SomeBlock name="A"> >> <SomeField name="A1">SomeValue1</SomeField> >> <SomeField name="A2">SomeValue9</SomeField> <-- Updated >> <SomeField name="A3">SomeValueA</SomeField> >> <SomeField name="A4">SomeValueX</SomeField> <-- Updated >> ...and about 50 more elements >> </SomeBlock> >> <SomeBlock name="C"> >> <SomeField name="A1">SomeValue5</SomeField> <-- Updated >> <SomeField name="A2">SomeValue1</SomeField> >> <SomeField name="A3">SomeValueF</SomeField> >> <SomeField name="A4">SomeValueE</SomeField> >> ...and about 50 more elements >> </SomeBlock> >> </Results> > > Here is an XSLT stylesheet: > > <xsl:stylesheet > xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > version="1.0"> > > <xsl:output method="xml" indent="yes"/> > <xsl:strip-space elements="*"/> > > <xsl:template match="@* | node()"> > <xsl:copy> > <xsl:apply-templates select="@* | node()"/> > </xsl:copy> > </xsl:template> > > <xsl:template match="/wrapper"> > <Results> > <xsl:apply-templates select="SomeBlock[@name = > /wrapper/actions/action[@type = 'Update']/@ref]"/> > </Results> > </xsl:template> > > <xsl:template match="SomeField"> > <xsl:copy> > <xsl:apply-templates select="@*"/> > <xsl:variable name="fieldaction" > select="/wrapper/actions/action[@type = 'Update' and @ref = > current()/parent::SomeBlock/@name]/field[@name = current()/@name]"/> > <xsl:choose> > <xsl:when test="$fieldaction"> > <xsl:value-of select="$fieldaction"/> > </xsl:when> > <xsl:otherwise> > <xsl:value-of select="."/> > </xsl:otherwise> > </xsl:choose> > </xsl:copy> > </xsl:template> > </xsl:stylesheet> > > > -- > > Martin Honnen --- MVP XML > http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
