xsl-list
[Top] [All Lists]

[xsl] Matching elements with specific multiple parents

2011-04-01 16:39:09
I'm trying to write a template match expression that matches an element only
if it is a child of (1 or more) parents.
Here's an example with 2 parents.
XML:
<?xml version="1.0"?>
<parent>
        <child a="1">
                <child a="2"/>
        </child>
        <foo>
                <child a="3"/>
        </foo>
        <bar>
                <child a="4"/>
        </bar>
</parent>

XSLT:
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

        <xsl:template match="text()"/>

        <xsl:template match="child[node-name(..) = ( QName((),'foo') ,
QName((),'bar') )]">
                <child a="{@a}"/>
                <xsl:apply-templates select="*"/>
        </xsl:template>
</xsl:stylesheet>


Result (correct)
<child a="3"/>
<child a="4"/>


-=--------------------------------------------------------------
This seems extremely verbose and inelegant but is the best I've come up
with.
I'd like something like 
   match="(foo|bar)/child"

but of course that doesn't work.

Any suggestions on a simpler syntax then what I've come up with ?
Note that this is programmatically generated XSLT so I cant easily
hand-optimize simple cases, it needs to work in the 1-N case in a way that's
reasonably constructible programmatically.
My working example is straightforward to construct but it hurts my eyes.
Maybe its just my eyes that needs work :)

Thanks for any ideas


----------------------------------------
David A. Lee
dlee(_at_)calldei(_dot_)com
http://www.xmlsh.org




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