Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


[xsl] Clone a Nodeset and then modify some Elements

From: "Alex Maddern" <alex@------------>
To:
Date: 9/3/2008 6:49:00 AM
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>

I have played with copying the entire SomeBlock into a Variable, but I'm
struggling to find a mechanism to updated any of the values without looping
through all the of the elements and writing them out unless they are the
Names in question and then writing my own element which just seems lengthy
to me. Isn't there a way to do something similar to the following:

<xsl:for-each select="/wrapper/actions/action">
    <xsl:variable name="actionTtype" select="@type">
    <xsl:variable name="blockName" select="@ref">
    <xsl:variable name="tempSomeBlock">
        <xsl:copy-of select="/wrapper/SomeBlock[@name='$blockName']" />
    </xsl:variable>
    <xsl:for-each select="field">
        <!-- Locate the SomeField named @name in $tempSomeBlock and update
it's value to . of SomeField -->
    </xsl:for-each>
    <xsl:copy-of select="tempSomeBlock" />
</xsl:for-each>
 
As you can see this isn't right and I was looking for a short sharp kick in
the pants to point me in the right direction.

Caveats - Obviously this is a watered down example which I think covers the
area I am having trouble with. The values that will replace the existing one
will come from various sources further down the document, not just within
the action\field elements

Any help or abuse greatly appreciated

Al


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