472,374 Members | 1,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 software developers and data experts.

Applying XSL templates dynamically

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/>
Jul 20 '05 #1
2 2021


Kimmo wrote:

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>

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)


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/

Jul 20 '05 #2
Martin Honnen <ma*******@yahoo.de> wrote in message news:<40********@olaf.komtel.net>...
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)


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, 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/>
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Vince C. | last post by:
Hi all, I've created XML documents that are described with a schema. I'm using those documents to create web pages. All my web pages contain a fixed header and a variable document part. The...
2
by: Thomas Sommer | last post by:
Hi, I think the following is not possible but maybe (hopefully) I am wrong: I have: <xsl:param name="test"> <tag1>asdfasdf</tag1> <tag2>asdfasdf</tag2> </xsl:param>
2
by: stb | last post by:
Hi. Is it possible to write inline Templates for a DataList dynamically in the HTML code, based on the DataSet that is bound to the DataList? Anyone know how?
6
by: Tim Meagher | last post by:
Can anyone help me figure out how to apply a stylesheet to a pushbutton defined in the asp:BoundColumn or asp:EditCommandColumn elements of a datagrid?
1
by: Ben R. | last post by:
Hi, I'm writing a .NET winforms app that serves as an email client. Users can store message templates. This is done via an XML document for loading and saving the templates. When the template is...
3
by: Christoph | last post by:
I'm still learning how to write stylesheets and the ones I've come up with for learning purposes are pretty simple and straightforward. I can get it to work, but probably not in the most ideal...
2
by: bogdan | last post by:
Hi, Can a single GridView be 'connected' to DetailsView that renders itself differently based on the currently selected row? I have a GridView with rows that could be displayed in the same way...
0
by: Craig Buchanan | last post by:
I am adding templates to a gridview dynamically. these columns are based on data values that are generated prior to calling the gridviews databind. the challenge is when the form does a...
2
by: Hvid Hat | last post by:
Hi When using <xsl:apply-templates select="Document"I get 10 documents. How can I limit it to only the first 5 documents, e.g. pseduo <xsl:apply-templates select="Document">
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.