473,395 Members | 2,689 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,395 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 33464
> <?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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...

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.