Generating new element names from old element names

U

UpgradeMyBrain

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!
 
R

Richard Tobin

<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
 
P

p.lepin

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>
 

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
474,006
Messages
2,570,265
Members
46,860
Latest member
JeremiahCo

Latest Threads

Top