473,625 Members | 3,306 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing variable node name to XSL "select" clause

62 New Member
Hello Everyone.
I'm new to XSL and couldn't get something to work. [Not sure if this is the right way to go about it...]
Sample XML excerpt:
Expand|Select|Wrap|Line Numbers
  1. <root>
  2. <ObjectTemplate>
  3. <NodeType1></NodeType1>
  4. <NodeType2></NodeType2>
  5. </ObjectTemplate>
  6. <Object>
  7. <NodeType1>N11</NodeType1>
  8. <NodeType2>N12</NodeType2>
  9. </Object>
  10. <Object>
  11. <NodeType1>N21</NodeType1>
  12. <NodeType2>N22</NodeType2>
  13. </Object>
  14. </root>
  15.  
My intent is to build a list of drop downs (# of drop downs = # of NodeTypes) based on those defined in the template. Using "xsl:for-each" and "xsl:elemen t", I could build 2 drop downs. But I cannot figure out how to pass the select clause a different node type.
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="/root/ObjectTemplate/*">
  2. <xsl:element name="select">
  3. <xsl:variable name="myNodeType"><xsl:value-of select="name(.)"/></xsl:variable>
  4. <xsl:for-each select="concat('/root/Object/',$myNodeType)">
  5. <xsl:element name="option"><xsl:value-of select="."/></xsl:element>
  6. </xsl:for-each>
  7. </xsl:element>
  8. </xsl:for-each>
  9.  
Line 4 is probably what is wrong in my approach. Any help in this regard is welcome.
Thanks in advance,
Parag
Mar 15 '08 #1
5 7131
jkmyoung
2,057 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="/root/ObjectTemplate/*">
  2. <xsl:element name="select">
  3. <xsl:variable name="myNodeType"><xsl:value-of select="name(.)"/></xsl:variable>
  4. <xsl:for-each select="/root/Object/*[local-name()=$myNodeType]">
  5. <xsl:element name="option"><xsl:value-of select="."/></xsl:element>
  6. </xsl:for-each>
  7. </xsl:element>
  8. </xsl:for-each>
  9.  
Mar 17 '08 #2
paragpdoke
62 New Member
Hello jkmyoung.
Thank you very much for the reply. It is working for me.
[Apologies for the delayed response. Somehow instant email notification preference is not working for my thescripts login.]

Regards,
Parag P. Doke
http://paragpdoke.blog spot.com
Mar 21 '08 #3
paragpdoke
62 New Member
I am sorry for reposting. But...my attempts to get only distinct values populated in the drop downs went futile. Here's the new code:
Expand|Select|Wrap|Line Numbers
  1. <root>
  2. <ObjectTemplate>
  3. <NodeType1></NodeType1>
  4. <NodeType2></NodeType2>
  5. </ObjectTemplate>
  6. <Object>
  7. <NodeType1>N11</NodeType1>
  8. <NodeType2>N12</NodeType2>
  9. </Object>
  10. <Object>
  11. <NodeType1>N21</NodeType1>
  12. <NodeType2>N22</NodeType2>
  13. </Object>
  14. <Object>
  15. <NodeType1>N11</NodeType1>
  16. <NodeType2>N22</NodeType2>
  17. </Object>
  18. </root>
  19.  
The XSL I was trying to use:
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="/root/ObjectTemplate/*">
  2. <xsl:element name="select">
  3. <xsl:variable name="myNodeType"><xsl:value-of select="name(.)"/></xsl:variable>
  4. <xsl:for-each select="/root/Object/*[local-name()=$myNodeType and not(.=preceding::$myNodeType)]">
  5. <xsl:element name="option"><xsl:value-of select="."/></xsl:element>
  6. </xsl:for-each>
  7. </xsl:element>
  8. </xsl:for-each>
  9.  
I also tried
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="/root/Object/*[local-name()=$myNodeType and not(.=preceding::local-name(.))]">
...but that too did not work :-(.

How can I get the drop downs to have only distinct values ? [Pardon my lack of knowledge, I have copied the preceding:: thing from elsewhere. Still reading XSL tutorials to get to know more about axes.]

Also, my 2nd question is: What is the difference between local-name() and name() functions ? When I tried using name(), it worked in similar manner.

Someone told me that it made sense to create a DTD file for all possible values instead of generating a list of distinct ones from the data in the XML file. But in this context of building the drop downs and populating them from a DTD, I have more questions:
- How to capture information inside DTD using XSL ?
- How to restrict values of a node to some pre-defined set ? [http://www.thescripts. com/forum/thread785602.ht ml]

Thanks in advance once again,
Parag P. Doke
Mar 22 '08 #4
jkmyoung
2,057 Recognized Expert Top Contributor
Close:
Expand|Select|Wrap|Line Numbers
  1. <xsl:for-each select="/root/Object/*[local-name()=$myNodeType and not(.=preceding::*[local-name()=$myNodeType])]">
  2.  
name() only gives you something different if it has a namespace attached.

For example, take node: <xsl:value-of/>
name is xsl:value-of
local-name is value-of


I'm not really good at DTD, but will have a look at the other question.
Mar 24 '08 #5
paragpdoke
62 New Member
Hi again.
Thank you for the explanation regarding name() and local-name().
Also, if I run into something helpful, I'll make sure I add to this post.

Regards,
Parag P. Doke

P.S.: I guess I should have had the title of this post as "Generating the select clause on the fly" rather than "Passing variable node name to XSL "select" clause".
Mar 25 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
11499
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold { CarsSoldID int (primary key) MonthID int DealershipID int NumberCarsSold int
1
5031
by: Son KwonNam | last post by:
When I try <xsl:value-of select="count('/GROUPS/GROUP')"/> on JSTL <x:transform> tag, I got the following error message. org.apache.jasper.JasperException: Can not convert #STRING to a NodeList! but, if I try that on Sonic XIS XSL transform API It works fine. What's the problem? Java(apache JSTL) does not support XPath functions?
10
5613
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I create a single View and specify also in this View the WHERE clause that is common in these stored procedures, I will have the new stored procecures changed to be like:
1
4167
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
6
9333
by: GSteven | last post by:
(as formerly posted to microsoft.public.access.forms with no result) I've created a continuous form which is based on a straightforward table (ex - customers - 100 records). On the form there is a checkbox with a control source named "MARK" (boolean) from customer table. I can check and uncheck individual records fine. Then I created 2 command buttons named "Select All" and "Deselect All". The Onclick property of these buttons runs code...
8
10308
by: Jean | last post by:
Hello all, I have the following data, that was queried and sorted to columns PROBLEM_ID and then by STATUSDATE (ascending): STATUS_ID STATUSDATE PROBLEM_ID --------- ---------- ---------- 10 12/04/2005 1234 40 15/05/2005 1234
2
1465
by: cephelo | last post by:
I have no problems outputting the attribute value when the node is in context, for example, @id when an <status> node is in context. However, I am having trouble outputting it in a <xsl:value-of /> element: <xsl:value-of select="procresults/settings@priority /> where it looks like
2
1881
by: junkblog747 | last post by:
What will the expression "/.." in the attribute select="/.." return? I just inherited a huge stylesheet to maintain which has a lot of parameters such as <xsl:param name="myParam" select="/.."/>. And then there is a lot of conditional processing based on this parameter. I stepped thru the code with a debugger in Oxygen and saw that the param is always empty (0 nodes selected). I am trying to figure what the original author of the...
3
3985
by: yawnmoth | last post by:
//form//input //form//select//option I can combine those with an or (|), but to make my code more concise, would it be possible to reuse the //form bit? Could I maybe do something like this, instead?: //form//(input or select//option) That doesn't work, but is there something similar that would? Or am I
0
8256
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
8189
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,...
1
8356
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
8497
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
7184
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...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.