473,795 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 >subelementTw o</subelementName>
<subelements>
<subelementOn e>
<value>one</value>
</subelementOne>
<subelementTw o>
<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:styleshe et 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="subelem ents/*">
<xsl:with-param name="subelemen tName">
<xsl:value-of select="subelem entName"/>
</xsl:with-param>
</xsl:apply-templates>
</resultroot>
</xsl:template>
<xsl:template match="subeleme ntOne">
<xsl:param name="subelemen tName"/>
<xsl:if test="name(.) = $subelementName ">
<!-- do something -->
</xsl:if>
</xsl:template>
<xsl:template match="subeleme ntTwo">
<xsl:param name="subelemen tName"/>
<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 2091


Kimmo wrote:

Basically the document looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<rootelement>
<subelementName >subelementTw o</subelementName>
<subelements>
<subelementOn e>
<value>one</value>
</subelementOne>
<subelementTw o>
<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:styleshe et 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="subelem ents/*">
<xsl:with-param name="subelemen tName">
<xsl:value-of select="subelem entName"/>
</xsl:with-param>
</xsl:apply-templates>
</resultroot>
</xsl:template>
<xsl:template match="subeleme ntOne">
<xsl:param name="subelemen tName"/>
<xsl:if test="name(.) = $subelementName ">
<!-- do something -->
</xsl:if>
</xsl:template>
<xsl:template match="subeleme ntTwo">
<xsl:param name="subelemen tName"/>
<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="subelem ents/*[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*******@yaho o.de> wrote in message news:<40******* *@olaf.komtel.n et>...
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="subelem ents/*[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
2570
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 header is the same in each page and is described in an XML document, "Head.xml". The document part, which is variable in content, is described in other XML files (e.g. "Document.xml", "Product.xml", "Register.xml").
2
2019
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
1315
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
4159
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
1051
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 loaded, it's displayed in the message textbox. Currently, this is the end of the XML phase. Now the user can edit the textbox before sending the message and the content of the textbox will be sent when the user hits send. Now to thicken the...
3
1410
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 way. I have a node in the XML I'm transforming that contains CRLFs. I did a search on how to do a search/replace but am not 100% sure how to fit it into my template. Here is a sample xml record: <UserRecordRoot> <UserItems>
2
2516
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 in the view (like a report) but when a user wants to edit/insert items I'd like to show a different layout and different controls based on the selected item type. For example, for some items I'd like to provide text boxes and for others I'd like...
0
1266
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 postback. i need to recreate these templates. until now, i've been storing the data needed to create the dynamic columns in a session variable. i was hoping to use the viewstate, but the viewstate isn't loaded until page_load. the gridview's templates...
2
2209
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
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.