Parsing node names

A

Alex

Hello,

I don't have sufficient experience with XSLT, and would really
appreciate somebody's help in me giving ideas on solving a problem I
have. Let's consider the following XML file:

<RootNode>
<RootNode:SubNode1>
<RootNode:SubNode1:SubNode1>...</RootNode:SubNode1:SunNode1>
</RootNode:SubNode1>
<RootNode:SubNode2>
<RootNode:SubNode2:SubNode1>...</RootNode:SubNode1:SunNode1>
<RootNode:SubNode2:SubNode2>...</RootNode:SubNode1:SunNode2>
</RootNode:SubNode2>
. . .
</RootNode>

This file has only 2 known features:
1. The name of the root node is RootNode
2. We DON'T KNOW the names of the children nodes in advance. We do
know, however, that they are formed by combining the name of the parent
with the name of the child; colon is used as a separator.

I need to format the data and to get rid of long node names (leaving
only the last part of the name):

RootNode
SubNode1
SubNode1 -> Node's data
SubNode2
SubNode1 -> Node's data
SubNode1 -> Node's data

Since the nodes names are not known in advance, I assume there is no a
straighforward way of using XSLT templates for processing. I would
appreciate your ideas.

Thanks a lot!

Alex.
 
D

David Carlisle

Alex said:
Hello,

I don't have sufficient experience with XSLT, and would really
appreciate somebody's help in me giving ideas on solving a problem I
have. Let's consider the following XML file:

<RootNode>
Note the root node in Xpath is / representing the document of the
whole document it is the parent of the top level element, and never has
a name.
<RootNode:SubNode1>
<RootNode:SubNode1:SubNode1>...</RootNode:SubNode1:SunNode1>
</RootNode:SubNode1>
<RootNode:SubNode2>
<RootNode:SubNode2:SubNode1>...</RootNode:SubNode1:SunNode1>
<RootNode:SubNode2:SubNode2>...</RootNode:SubNode1:SunNode2>
</RootNode:SubNode2>
. . .
</RootNode>

This file has only 2 known features:
1. The name of the root node is RootNode
2. We DON'T KNOW the names of the children nodes in advance. We do
know, however, that they are formed by combining the name of the parent
with the name of the child; colon is used as a separator.

If the colon is used without declaring namespaces for the prefix then
the input does not conform to the xml namespaces specification (and
contravenes a strong hint in the core xml spec that you should not do
that) as such the file may not be used with Xpath/xslt/XML-schema
or any other Namespace aware XML tool.

If you ran some text transform such as
sed -e "s/:/_/g"
to change all : to _ so that you could parse the input with a namespace
aware parser, then you could use xslt to do the transform that you
require in a fairly straight forward way, you'd just need someththing
like
<xsl:template match="*">
<xsl:element name="substring-after(name(),concat(name(..),'_'))">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
I need to format the data and to get rid of long node names (leaving
only the last part of the name):

RootNode
SubNode1
SubNode1 -> Node's data
SubNode2
SubNode1 -> Node's data
SubNode1 -> Node's data

Since the nodes names are not known in advance, I assume there is no a
straighforward way of using XSLT templates for processing. I would
appreciate your ideas.

Thanks a lot!

Alex.


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top