Parsing node names

Discussion in 'XML' started by Alex, Jul 8, 2005.

  1. Alex

    Alex Guest

    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.
    Alex, Jul 8, 2005
    #1
    1. Advertising

  2. "Alex" <> writes:

    > 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>
    </xsl:template>


    >
    > 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
    David Carlisle, Jul 8, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,413
  2. Lewis G. Pringle, Jr.
    Replies:
    0
    Views:
    592
    Lewis G. Pringle, Jr.
    Sep 30, 2003
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,404
    Dimitre Novatchev
    Aug 24, 2006
  4. thecolour
    Replies:
    0
    Views:
    369
    thecolour
    Jun 26, 2007
  5. thecolour
    Replies:
    2
    Views:
    412
    thecolour
    Jun 27, 2007
Loading...

Share This Page