Connecting Tech Pros Worldwide Forums | Help | Site Map

Applying XSL templates dynamically

Kimmo
Guest
 
Posts: n/a
#1: Jul 20 '05
Folks,

I have a XML document that has been put together from a "dynamic part"
(generated somehow during runtime) and a "static part" (read from a
control file). Basically the document looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<rootelement>
<subelementName>subelementTwo</subelementName>
<subelements>
<subelementOne>
<value>one</value>
</subelementOne>
<subelementTwo>
<value>foo</value>
<anotherValue/>
</subelementTwo>
</subelements>
</rootelement>

Here the dynamic part is the content of the <subelementName> element,
while the subelements come from the control file.

Now I would want to have the dynamic part of the document to control
the transformation, i.e. i am only interrested of the subelement which
name equals the content of the <subelementName> element.

I think i can achieve this by the following xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
indent="yes"/>
<xsl:template match="/rootelement">
<resultroot>
<xsl:apply-templates select="subelements/*">
<xsl:with-param name="subelementName">
<xsl:value-of select="subelementName"/>
</xsl:with-param>
</xsl:apply-templates>
</resultroot>
</xsl:template>
<xsl:template match="subelementOne">
<xsl:param name="subelementName"/>
<xsl:if test="name(.) = $subelementName">
<!-- do something -->
</xsl:if>
</xsl:template>
<xsl:template match="subelementTwo">
<xsl:param name="subelementName"/>
<xsl:if test="name(.) = $subelementName">
<!-- do something else-->
</xsl:if>
</xsl:template>
</xsl:stylesheet>


However, I would like to "limit the templates applied" instead of
apply them all and then try to figure out in each of them wheter the
template should provide something to the output or not (imagine, if we
had thousands of these templates).

So I quess I have these questions:

1.
How can i have the <xsl:apply-templates> to select only the node-set
that match to the content of an element (here: subelementName)

2.
Is there another, preferred way of doing this thing - am i looking at
wrong direction here?

Appreciate Your views on this,

<kimmo/>

Martin Honnen
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Applying XSL templates dynamically




Kimmo wrote:

[color=blue]
> Basically the document looks like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <rootelement>
> <subelementName>subelementTwo</subelementName>
> <subelements>
> <subelementOne>
> <value>one</value>
> </subelementOne>
> <subelementTwo>
> <value>foo</value>
> <anotherValue/>
> </subelementTwo>
> </subelements>
> </rootelement>
>[/color]

[color=blue]
> Now I would want to have the dynamic part of the document to control
> the transformation, i.e. i am only interrested of the subelement which
> name equals the content of the <subelementName> element.
>
> I think i can achieve this by the following xsl:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> <xsl:output method="xml" version="1.0" encoding="UTF-8"
> indent="yes"/>
> <xsl:template match="/rootelement">
> <resultroot>
> <xsl:apply-templates select="subelements/*">
> <xsl:with-param name="subelementName">
> <xsl:value-of select="subelementName"/>
> </xsl:with-param>
> </xsl:apply-templates>
> </resultroot>
> </xsl:template>
> <xsl:template match="subelementOne">
> <xsl:param name="subelementName"/>
> <xsl:if test="name(.) = $subelementName">
> <!-- do something -->
> </xsl:if>
> </xsl:template>
> <xsl:template match="subelementTwo">
> <xsl:param name="subelementName"/>
> <xsl:if test="name(.) = $subelementName">
> <!-- do something else-->
> </xsl:if>
> </xsl:template>
> </xsl:stylesheet>
>
>
> However, I would like to "limit the templates applied" instead of
> apply them all and then try to figure out in each of them wheter the
> template should provide something to the output or not (imagine, if we
> had thousands of these templates).
>
> So I quess I have these questions:
>
> 1.
> How can i have the <xsl:apply-templates> to select only the node-set
> that match to the content of an element (here: subelementName)[/color]

Why can't you make the check you have later already in apply-templates,
somehow alike
<xsl:apply-templates select="subelements/*[local-name() =
current()/subelementName]" />
that should do (even if my attempt above might need some adjusting).




--

Martin Honnen
http://JavaScript.FAQTs.com/

Kimmo
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Applying XSL templates dynamically


Martin Honnen <mahotrash@yahoo.de> wrote in message news:<40eab9b0$1@olaf.komtel.net>...[color=blue][color=green]
> > So I quess I have these questions:
> >
> > 1.
> > How can i have the <xsl:apply-templates> to select only the node-set
> > that match to the content of an element (here: subelementName)[/color]
>
> Why can't you make the check you have later already in apply-templates,
> somehow alike
> <xsl:apply-templates select="subelements/*[local-name() =
> current()/subelementName]" />
> that should do (even if my attempt above might need some adjusting).[/color]

Martin, exactly. Thanks.

My question was "how", You did show me "how" although You answered
with a question "why". It is easy to answer to that question (of
Yours): there is no reason at all why I wouldn't do it the way You
suggested - other than that I was clumsy and sloppy with the
expression in the square brackets. I tried and i tried and i read more
of Jeni Tennison's book and tried again, but no success. Of course now
it looks self-evident and Your suggestion was basically exactly what I
was looking for.

Thanks again.

<kimmo/>
Closed Thread