473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Using one template to process different nodes?

Hello,

I am a novice with XSL, and can't quite figure out how to come up with
a solution for the following problem. Let's assume we have the
following set of XML nodes:

<DataBlocks>
<AlphaBlock>
<AlphaBlock.Name>Alpha<AlphaBlock.Name/>
<AlphaBlock.Value>1<Alpha.Block.Value/>
<AlphaBlock/>
<BetaBlock>
<BetaBlock.Name>Beta</BetaBlock.Name/>
<BetaBlock.Value>2<BetaBlock.Block.Value/>
<BetaBlock/>
<DataBlocks/>

Alpha and Beta blocks contain information that can be processed in
identical way, therefore I intend to utilize one template, something
like:

<xsl:template match="DataBlocks//AlphaBlock | DataBlocks//BetaBlock">
....
<xsl:template/>

Within that template, I want to create a variable, which would hold the
name of the node:

<xsl:variable name="BlockType">
<xsl:choose>
<xsl:when test = "contains(current(),
'BetaBlock')>BetaBlock</xsl:when>
<xsl:otherwise>AlphaBlock</xsl:otherwise>
</xsl:choose>
</xsl:variable>

My goal is then to use that variable to access both AlphaBlock.Value
and BetaBlock.Value nodes in something like:

Value is <xsl:value-of select="concat($BlockType,'.Value')"/>, but
certainly it does not work.

I would appreciate your help.

Thanks.

Alex.

Dec 19 '05 #1
4 1252
In article <11**********************@g14g2000cwa.googlegroups .com>,
<af******@yahoo.com> wrote:
Alpha and Beta blocks contain information that can be processed in
identical way, therefore I intend to utilize one template, something
like:

<xsl:template match="DataBlocks//AlphaBlock | DataBlocks//BetaBlock">
...
<xsl:template/>
Yes, that's reasonable.
Within that template, I want to create a variable, which would hold the
name of the node:

<xsl:variable name="BlockType">
<xsl:choose>
<xsl:when test = "contains(current(),
'BetaBlock')>BetaBlock</xsl:when>
<xsl:otherwise>AlphaBlock</xsl:otherwise>
</xsl:choose>
</xsl:variable>
You are testing whether the the text content of the element contains the
string "BetaBlock" which doesn't correspond to what you said you wanted.
The simplest way to get the name of the element into the variable is
to do something like

<xsl:variable name="BlockType" select="name()"/>
My goal is then to use that variable to access both AlphaBlock.Value
and BetaBlock.Value nodes in something like:

Value is <xsl:value-of select="concat($BlockType,'.Value')"/>, but
certainly it does not work.


You can't dynamically create a pattern to match. But you can do
something like this:

<xsl:value-of select="*[name() = concat($BlockType, '.Value')"/>

That you are having to do this suggests that the design of your
document is not optimal. Why not simply have

<AlphaBlock>
<Name>Alpha</Name>
<Value>1</Value>
<AlphaBlock/>

Or even <Block type="Alpha">

-- Richard
Dec 19 '05 #2
Richard,

Thanks. I will try your suggestion tomorrow.

As for the design of the original XML document, I can't argue - it's
far from being optimized, but it's a legacy document, and its structure
can't be changed at this point.

Alex.

Dec 20 '05 #3
In article <11*********************@g49g2000cwa.googlegroups. com>,
<af******@yahoo.com> wrote:
As for the design of the original XML document, I can't argue - it's
far from being optimized, but it's a legacy document, and its structure
can't be changed at this point.


If you're going to have to do a lot of work on these documents, you
might consider writing a stylesheet to first convert it into a more
tractable form, rather than constantly dealing with the original.

-- Richard
Dec 20 '05 #4
<xsl:variable name="BlockType" select="local-name(.)"/> is enough

<xsl:value-of select="*[local-name(.)=concat( $BlockType, '.Name' )]"/>

<xsl:value-of select="*[local-name(.)=concat( $BlockType, '.Value' )]"/>

Best regards,

A.Brillant
EditiX - XML Editor and XSLT Debugger
http://www.editix.com

af******@yahoo.com wrote:
Hello,

I am a novice with XSL, and can't quite figure out how to come up with
a solution for the following problem. Let's assume we have the
following set of XML nodes:

<DataBlocks>
<AlphaBlock>
<AlphaBlock.Name>Alpha<AlphaBlock.Name/>
<AlphaBlock.Value>1<Alpha.Block.Value/>
<AlphaBlock/>
<BetaBlock>
<BetaBlock.Name>Beta</BetaBlock.Name/>
<BetaBlock.Value>2<BetaBlock.Block.Value/>
<BetaBlock/>
<DataBlocks/>

Alpha and Beta blocks contain information that can be processed in
identical way, therefore I intend to utilize one template, something
like:

<xsl:template match="DataBlocks//AlphaBlock | DataBlocks//BetaBlock">
...
<xsl:template/>

Within that template, I want to create a variable, which would hold the
name of the node:

<xsl:variable name="BlockType">
<xsl:choose>
<xsl:when test = "contains(current(),
'BetaBlock')>BetaBlock</xsl:when>
<xsl:otherwise>AlphaBlock</xsl:otherwise>
</xsl:choose>
</xsl:variable>

My goal is then to use that variable to access both AlphaBlock.Value
and BetaBlock.Value nodes in something like:

Value is <xsl:value-of select="concat($BlockType,'.Value')"/>, but
certainly it does not work.

I would appreciate your help.

Thanks.

Alex.

Dec 21 '05 #5

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

Similar topics

3
by: ppl | last post by:
I'm very new to XSL and have come across a stumbling block with a recent assignment at work. I need to translate from XML to XML using an XSL style sheet. Here is the input XML: <?xml...
2
by: RanDeep | last post by:
I have two nodes that both exist underneath the root node. They are linked, however, in the sense that one of the nodes contains a copy of an id that is used to refer to the other. However, when I...
6
by: Ramon M. Felciano | last post by:
Helo all -- I'm trying to gain a deeper understand for what type of semi-declarative programming can be done through XML and XPath/XSLT. I'm looking at graph processing problems as a testbed for...
10
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in...
4
by: Mastadex | last post by:
So here is my Code: template <class T> struct Nodes { T *Data; // The Image char Name; // The Name of the image Nodes<T> *Prev; Nodes<T> *Next;
0
by: mookie | last post by:
m looking to create something similar to #region and #endregion using treeviews the problem is that instead of #region, i am using ;fold and ;endfold i am also allowed ot have a fold within...
3
by: Jeff Calico | last post by:
Hello everyone I am transforming an XML document to text, basically only outputting a small portion of it. When I run the following XSLT via Xalan's processor, I get a bunch of unwanted blank...
8
by: Jimbo | last post by:
Hello I am currently designing an internal ordering system for IT equipment. I am designing it in ASP.NET (vb) using Visual Studio 2003 and using Microsoft SQL Server I have got the system...
10
by: William Krick | last post by:
I am writing an XSL transform that converts XML data about vehicles into XML data that will fill printed forms. The default form can handle up to 5 vehicles which I handle using subscripts... ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.