Generating new element names from old element names

Discussion in 'XML' started by UpgradeMyBrain@gmail.com, Oct 19, 2006.

  1. Guest

    Hi.

    Does anyone know how to take an input XML file, extract just a section
    of the element name and generate a new element name with part of that
    name? Example:

    Input file contains:
    <Names>
    <Name_is_John>
    <DOB>11/12/1995</DOB>
    <Residence>New York City</Residence>
    </Name_is_John>
    <Name_is_Jane_Bloggs>
    <DOB>11/20/1995</DOB>
    <Residence>Boston</Residence>
    <Name_is_Jane_Bloggs>
    </Names>

    The output should look like:

    <Names>
    <John>
    <DOB>11/12/1995</DOB>
    <Residence>New York City</Residence>
    </John>
    <Jane_Bloggs>
    <DOB>11/20/1995</DOB>
    <Residence>Boston</Residence>
    <Jane_Bloggs>
    </Names>

    To generate the correct element names, I've tried using a command like:

    <xsl:element name="substring-after('.','Name_is')" />

    Actually, I've not tried it with a node argument, because I'm just
    trying to get the function to work with a regular ol' string. When
    trying to generate output, I get an error. The idea is to take data
    with arbitrary names that will all be prefixed in a precise way and
    strip out that prefix. Any ideas?

    Thanks!
     
    , Oct 19, 2006
    #1
    1. Advertising

  2. In article <>,
    <> wrote:

    > <Name_is_John>
    >[...]
    > <John>


    >To generate the correct element names, I've tried using a command like:
    >
    ><xsl:element name="substring-after('.','Name_is')" />


    That looks for the part of the string '.' that's after 'Name_is'.
    Presumably you meant

    substring-after(.,'Name_is')

    (without the quotes) which would find that part of the text content
    of the current node. But what you really seem to want is something like:

    substring-after(local-name(), 'Name_is_')

    -- Richard
     
    Richard Tobin, Oct 19, 2006
    #2
    1. Advertising

  3. Guest

    wrote:
    > Does anyone know how to take an input XML file, extract
    > just a section of the element name and generate a new
    > element name with part of that name? Example:
    >
    > Input file contains:
    > <Names>
    > <Name_is_John>
    > <DOB>11/12/1995</DOB>
    > <Residence>New York City</Residence>
    > </Name_is_John>
    > <Name_is_Jane_Bloggs>
    > <DOB>11/20/1995</DOB>
    > <Residence>Boston</Residence>
    > <Name_is_Jane_Bloggs>


    This is not well-formed. You're asking for help. Is it that
    hard to make sure that your sample XML is at least
    well-formed before asking for help? It's okay if you can't
    figure something out and ask for help. But I would hope you
    at least can figure out well-formedness.

    My usual policy is to offer help but omit any in-depth
    explanations for people with sloppy examples. Other people
    on the newsgroup might be kinder than that, but I wouldn't
    count on it.

    > </Names>
    >
    > The output should look like:
    >
    > <Names>
    > <John>
    > <DOB>11/12/1995</DOB>
    > <Residence>New York City</Residence>
    > </John>
    > <Jane_Bloggs>
    > <DOB>11/20/1995</DOB>
    > <Residence>Boston</Residence>
    > <Jane_Bloggs>
    > </Names>
    >
    > To generate the correct element names, I've tried using a
    > command like:
    >
    > <xsl:element name="substring-after('.','Name_is')" />


    You need identity transformation and attribute value
    templates.

    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <!-- identity -->
    <xsl:template match="node()|@*">
    <xsl:copy>
    <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
    </xsl:template>
    <!-- exception -->
    <xsl:template
    match="*[starts-with(local-name(),'Name_is_')]">
    <xsl:element
    name="{substring-after(local-name(),'Name_is_')}">
    <xsl:apply-templates select="node()|@*"/>
    </xsl:element>
    </xsl:template>
    </xsl:stylesheet>

    --
    Pavel Lepin
     
    , Oct 20, 2006
    #3
    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. Carl
    Replies:
    0
    Views:
    541
  2. Jim Hill
    Replies:
    3
    Views:
    428
    Jim Hill
    Feb 12, 2007
  3. *Prot3anThr3ad*

    old repository for old C++ source code

    *Prot3anThr3ad*, Sep 29, 2006, in forum: C++
    Replies:
    6
    Views:
    399
    *Prot3anThr3ad*
    Oct 2, 2006
  4. jakester
    Replies:
    3
    Views:
    1,904
    jakester
    Apr 3, 2007
  5. John Henry
    Replies:
    24
    Views:
    1,043
    alex23
    May 30, 2008
Loading...

Share This Page