xsl-list
[Top] [All Lists]

Re: [xsl] Clone a Nodeset and then modify some Elements

2008-09-03 03:32:18
Alex Maddern wrote, On 2008-09-03 08:48:
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

[...]

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[(_at_)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>
Variables in XSLT are single-assignment so you cannot update the content of the variable within the scope it was created. One solution is to implement a recursive function that builds the sequence item for item for each recursive call and when you reach the and of the input sequence you return the resulting sequence back to the top.

BTW, nodeset is a concept from XSLT 1.0, which has been replaced with sequences in XSLT 2.0. See www.w3.org for more information. If you are using XSLT 1.0, you need to implement the recursive function as a named template, whereas in XSLT 2.0 you can implement it as a function.
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--
Carpe Diem!
===
Jonas Mellin, Assistant Professor in Computer Science
School of Humanities and Informatics, Building E-2
University of Skövde, P.O. Box 408, SE-541 28 Skövde, Sweden
Phone: +46 500 448321, Fax: +46 500 448399
Email: jonas(_dot_)mellin(_at_)his(_dot_)se, URL: http://www.his.se/melj


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>