473,219 Members | 1,816 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,219 software developers and data experts.

extract element names with XSLT

Hi,
I've got the following simple XSLT stylesheet, that lists all the values of
the elements of any given XML file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/">
<xsl:value-of select="current()"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

What I'm trying to find now is a way to extract the element name as well
(e.g. "TITLE" if the element is called <TITLE>).

Does anybody know how to do this?

Thanks,
Stefan
Jul 20 '05 #1
9 33448
> <?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="/"> <xsl:value-of select="local-name()"/> <xsl:value-of select="current()"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Quot capita, tot sententiae" - Terentius , Phormio 454
Jul 20 '05 #2
Thanks, but I'm afraid that I was cheering too early. I noticed that
"current()" selects all the values and not only one (so the for-each isn't
necessary).

Is there a way to iterate through the element names and element values one
after the other (without knowing the structure of the XML file)?

Thanks,
Stefan
Jul 20 '05 #3
> Thanks, but I'm afraid that I was cheering too early. I noticed that
"current()" selects all the values and not only one (so the for-each isn't
necessary).

Is there a way to iterate through the element names and element values one
after the other (without knowing the structure of the XML file)?


Yes, but you have to realize that an element's value consist of the concatenation off all child text nodes up to any dept level.

Suppose you have this XML:
<root>
<title>hello</title>
<section>
<node>this is some <i>italic</i> text</node>
</section>
</root>
If you want this output:
root:hellothis is some italic text
title:hello
section:this is some italic text
node:this is some italic text
i:italic

Use:
<xsl:template match="/">
<xsl:for-each select="//*">
<xsl:value-of select="local-name()"/>:<xsl:value-of select="current()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>

If you'd rather need this output:
root:
title:hello
section:
node:this is some text
i:italic

Use:
<xsl:template match="/">
<xsl:for-each select="//*">
<xsl:value-of select="local-name()"/>:<xsl:apply-templates select="current()/text()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
If you need yet another variant, just ask it.

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
"Quot capita, tot sententiae" - Terentius , Phormio 454
Jul 20 '05 #4
> If you'd rather need this output:
root:
title:hello
section:
node:this is some text
i:italic

Use:
<xsl:template match="/">
<xsl:for-each select="//*">
<xsl:value-of select="local-name()"/>:<xsl:apply-templates
select="current()/text()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>

Thanks very much for this code. I just wondered if I was on the right path
with this code (acutally the second one) when I need an 'exact copy' of the
XML file. What I'm actually trying to do is to produce a stylesheet, that
just copies the XML file (I mustn't use xsl:copy and xsl:copy-of, though).

I cannot just put this code inside the for-each statement for sure:

<xsl:variable name="localname" value-of="local-name()" />
<xsl:variable name="text" value-of="current()/text()" />

<xsl:element name="%localname;">
<xsl:text>%text;</xsl:text>
</xsl:element>

Could you give me a hint on how to solve that please?

Thanks,
Stefan
Jul 20 '05 #5
Tempore 14:42:52, die Saturday 08 January 2005 AD, hinc in foro {comp.text.xml} scripsit Stefan Franke <st***********@gmx.co.uk>:
What I'm actually trying to do is to produce a stylesheet, that
just copies the XML file (I mustn't use xsl:copy and xsl:copy-of, though).
Why not use them?
I cannot just put this code inside the for-each statement for sure:

<xsl:variable name="localname" value-of="local-name()" />
<xsl:variable name="text" value-of="current()/text()" />

<xsl:element name="%localname;">
<xsl:text>%text;</xsl:text>
</xsl:element>


You might put this:
<xsl:variable name="localname" select="local-name()" />
<xsl:variable name="text" select="current()/text()" />

<xsl:element name="{$localname}">
<xsl:value-of select="text()"/>
</xsl:element>

or more simple:

<xsl:element name="{local-name()}">
<xsl:apply-templates select="current()/text()"/>
</xsl:element>

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
Jul 20 '05 #6
> <xsl:element name="{local-name()}">
<xsl:apply-templates select="current()/text()"/>
</xsl:element>


The problem with this approach is that an element is opened and right
afterwards closed again. So I cannot generate a tree structure with this
code. It's as if there should be some kind of recursive call or something
like that...

regards,
Stefan
Jul 20 '05 #7
Tempore 16:22:40, die Saturday 08 January 2005 AD, hinc in foro {comp.text.xml} scripsit Stefan Franke <st***********@gmx.co.uk>:
The problem with this approach is that an element is opened and right
afterwards closed again. So I cannot generate a tree structure with this
code.


Yes you can, creating tree strucures is perfectly possible in XSLT.

The 'xsl:apply-templates' will help you achieve it. You should abandon the use of 'xsl:for-each' when you're dealing with recursion.

This code will make a pseudo copy of the input XML (only elements and text nodes are copied):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
Jul 20 '05 #8
> The 'xsl:apply-templates' will help you achieve it. You should abandon the
use of 'xsl:for-each' when you're dealing with recursion.

Ah, I see now how that cannot works with 'for-each'. Where I would use
'for-each' though, is when I want to extract the attributes:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="*">
<xsl:element name="{local-name()}">

<xsl:for-each select="current()/@*">
<xsl:variable name="i" select="string(current()/@*)" />
<xsl:attribute name="a">$i</xsl:attribute>
</xsl:for-each>

<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>

</xsl:stylesheet>

I've got two problems with this code. Firstly, I cannot get this variable $i
to show its value (I always get '$i' with Saxon 8.2) and secondly, how can I
retrieve the name of the attribute with an XPath query?

Thanks,
Stefan
Jul 20 '05 #9
> <xsl:for-each select="current()/@*">
<xsl:variable name="i" select="string(current()/@*)" />
<xsl:attribute name="a">$i</xsl:attribute>
</xsl:for-each>

I've got two problems with this code. Firstly, I cannot get this variable $i
to show its value (I always get '$i' with Saxon 8.2) If you want the value of something... use 'xsl:value-of' ...that does make sense:)

Use:
<xsl:variable name="i" select="string(.)" />
<xsl:attribute name="a"><xsl:value-of select="$i"/></xsl:attribute>
Or easier:
<xsl:attribute name="a"><xsl:value-of select="."/></xsl:attribute>
and secondly, how can I
retrieve the name of the attribute with an XPath query?

use local-name(): it works for attributes as well as elements.
So the whole code is this:
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:for-each select="current()/@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:for-each>
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:template>

but tell me, why not use 'xsl:copy-of' or 'xsl:copy'?

regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Vincit omnia simplicitas
Keep it simple
Jul 20 '05 #10

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

Similar topics

9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
3
by: Ray Tayek | last post by:
hi, trying to use an xslt to make an xslt. trying something like: <?xml version="1.0" encoding="UTF-8"?> <?xmlspysamplexml H:\java\projects\spy1\spy\inputDocumentMap.xml?> <xsl:stylesheet...
2
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2"...
2
by: pintihar | last post by:
I am trying to map external xml documents to a class in dotnet. The problem is that the elements of the input xml will have different names than the properties of the class. How do I create the...
4
by: ina | last post by:
Hello all, I am newbie in xml and have a problem with this parse. I have this xml.file <Style> <Strategy>
6
by: mandibdc | last post by:
I need to extract some elements from a very large XML file. Because of the size, I'd like to work with it on my Linux machine as a text file. Basically, I am going to have a list of specific...
8
by: bennett.matthew | last post by:
Hello all, This is probably an elementary (no pun intended) question, but I've spent all afternoon on it and it's driving me crazy. I have a function which dynamically adds to a table. It...
8
by: Scott Sauyet | last post by:
I found myself needing to find my way recursively through a document in XSLT, finding the dependencies one element had on others, and including only those in an initial set, their dependencies, the...
6
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

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.