473,406 Members | 2,620 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,406 software developers and data experts.

Select nodes from another XML document?

Hi,

Maybe I've got a strange question.
I've got a XML document (main.xml) with several nodes. Additional I've
another XML docuement (role.xml), with allowed nodes. In a XSL-File I
just would like to extract nodes from the main.xml file, which were
declared in role.xml.

Example:

main.xml:
<main>
<firstname>Tom</firstname>
<lastname>Jones</lastname>
<street>Sunset 11</street>
<state>CA</state>
....
</main>

Role.xml:
<role>
<field>lastname</field>
<field>state</field>
</role>

So I just want to extract "lastname" (Jones) and "state" (CA) from the
main.xml.

I just tried following XSLT on Main.xml:

<xsl:template match="/">
<xsl:apply-templates select="document(Role.xml)/role"/>
</xsl:template>
<xsl:template match="role">
<xsl:for-each select="field">
<xsl:value-of select="/main//current()"/>
</xsl:for-each>
</xsl:template>
In match="role" all "fields" nodes from Role.xml are traversing. So
now each found node (lastname, state) should be selected in main.xml
(/main/lastname and /main/state). The XSL should output "Jones" and
"CA". But it seems that I can't use current() in the "<xsl:value-of
select=...".

Does anyone know a solution?
Thanks a lot for helping!
Jul 20 '05 #1
5 1511


Chris wrote:

I've got a XML document (main.xml) with several nodes. Additional I've
another XML docuement (role.xml), with allowed nodes. In a XSL-File I
just would like to extract nodes from the main.xml file, which were
declared in role.xml.

Example:

main.xml:
<main>
<firstname>Tom</firstname>
<lastname>Jones</lastname>
<street>Sunset 11</street>
<state>CA</state>
...
</main>

Role.xml:
<role>
<field>lastname</field>
<field>state</field>
</role>

So I just want to extract "lastname" (Jones) and "state" (CA) from the
main.xml.

I just tried following XSLT on Main.xml:

<xsl:template match="/">
<xsl:apply-templates select="document(Role.xml)/role"/>
</xsl:template>
<xsl:template match="role">
<xsl:for-each select="field">
<xsl:value-of select="/main//current()"/>


You could try
<xsl:variable name="elementName" select="local-name()" />
<xsl:value-of select="/main/*[local-name() = $elementName]" />
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #2
Tempore 16:58:28, die Wednesday 16 March 2005 AD, hinc in foro {comp.text.xml} scripsit Chris <c-***@gmx.ch>:
<xsl:template match="/">
<xsl:apply-templates select="document(Role.xml)/role"/>
</xsl:template>
<xsl:template match="role">
<xsl:for-each select="field">
<xsl:value-of select="/main//current()"/>
</xsl:for-each>
</xsl:template>


try this:

<xsl:variable name="main" select="/" />

<xsl:template match="/">
<xsl:apply-templates select="document('Role.xml')/role"/>
</xsl:template>

<xsl:template match="role">
<xsl:for-each select="field">
<xsl:value-of select="$main//*[local-name()=current()]"/>
</xsl:for-each>
</xsl:template>

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Deserta faciunt et innovationem appelant
Jul 20 '05 #3
Thanks a lot!
It works :)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4
I've just have one more question.
If I would like to declare a node-set in Role.xml, the above code
doesn't work for that scenario.

In the following example I've added in Role.xml the parents node with
its children "mother", "father" and "street". So now I would like to
declare "parents/street" in the role.xml as a field. So the XSL should
also output "Oldstreet 99". But it seems that local-name() doesn't work
for a node-sets.

example:

main.xml:
<main>
<firstname>tom</firstname>
<lastname>jones</lastname>
<street>Sunset 11</street>
<state>ca</state>
<parents>
<mother>Jenny</mother>
<father>Bill</father>
<street>Oldstreet 99</street>
</parents>

....
</main>

Role.xml:
<role>
<field>lastname</field>
<field>state</field>
<field>parents/street</field>
</role>
It would be great, if someone could clarify me!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
But it seems that local-name() doesn't work for a node-sets.


Your problem isn't to do with node sets (all nodes are in a node set in
Xpath) so local-name() _only_ works with node-sets, and gives the name
of the (only) node in the set.

Your problem is that you want to evaluate an Xpath expressed as a
string.
this is a FAQ over on xsl-list (see that list's faq) but basically
XSLT doesn't have such a function. It is the same problem if you had a C
program which is handed a string containing C syntax, you need to have
access to a parser and compiler at run time, and that isn't necessarily
available.

Some systems (eg saxon or systems supporting exslt dyn:evaluate) have an
extension function that can parse XPath.
in which case you could use saxon:evaluate(field)

Otherwise you can do simple cases by hand

if it is a single name
*[local-name()=$string]
if it has a path with exactly two names and no predicates:
*[local-name()=substring-before($string,'/')]/*[local-name()=substring-after($string,'/')]
etc

David
Jul 20 '05 #6

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

Similar topics

5
by: Colman | last post by:
Howdy all! I guess I'm a newbie, because I am stumped (or maybe just too durned tired). Here's what I got... CREATE TABLE `nodecat_map` ( `nodecat_id` mediumint(8) unsigned NOT NULL...
2
by: Cali | last post by:
Please bear with me, I have been reading about XSL for a couple hours. I have an XML document that contains multiple <page> tags interspersed throughout the tree. <text> .... <page>1</page>...
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
6
by: Paul J Lay | last post by:
I am using the XmlDocument Select Nodes method and it seems to work OK except that it returns embedded nodes in the collection when I only want the nodes that are not embedded. For example I have...
13
by: Oliver Hauger | last post by:
Hello, In my html form I show a select-element and if this element is clicked I fill it per JavaScript/DOM with option-elements. I use the following code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD...
4
by: praveen | last post by:
I have a form with treeview control loaded from xml document,text box, two buttons named "Find" and "FindNext" and my treeview which looks like below. Details |__ policy status |__ created by...
16
by: Brian D | last post by:
I have a multiple select list that is created dynamically based on a previous selection on an asp page. The first thing I do is to clear the curent option list by ...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
4
by: Patrick Nolan | last post by:
I am using javascript to manipulate optgroups in select elements. When the results are displayed in Firefox 2.0 there is an annoying blank line at the top of the multi-line select box. This...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.