Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Clone a Nodeset and then modify some Element

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 9/3/2008 2:23:00 PM

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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent