xsl-list
[Top] [All Lists]

Re: Adding Missing Elements

2005-09-13 12:48:36
Hi,

Tempore 18:01:35, die 09/13/2005 AD, hinc in 
xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com scripsit Emerson, Matt 
<Matt(_dot_)Emerson(_at_)FMR(_dot_)COM>:

Given these possible variations and based on some processing elsewhere
in the document, I would like to make sure that <node> always has
<item>B</item> in its list...possibly adding <list> if it is not
present.

Can someone give me some suggestions on the best way to do this?
"The best way to do this" does not exist, it's amatter of personal personal 
style.

Here's a solution that I perceive as most elegant:

<xsl:stylesheet version="1.0" xmlns:var="myvar"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        
<xsl:output method="xml" indent="yes"/>

<var:root>
        <list>
                <item>B</item>
        </list>
</var:root>

<xsl:variable name="var" select="document('')/xsl:stylesheet/var:root/*"/>

<xsl:template match="node()|@*">
        <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
        </xsl:copy>
</xsl:template>

<xsl:template match="node|list|item">
        <xsl:element name="{local-name()}">
                <xsl:apply-templates select="@*" />
                <xsl:apply-templates select="
                        $var[current()/self::node][not(current()/list)]|
                        $var[current()/self::list]/item[not(.=current()/item)]|
                        node()" />
        </xsl:element>
</xsl:template>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
«Et ipsa scientia potestas est»  - Francis Bacon , Meditationes sacrae

--~------------------------------------------------------------------
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>