Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Generating new element names from old element names

Reply
Thread Tools

Generating new element names from old element names

 
 
UpgradeMyBrain@gmail.com
Guest
Posts: n/a
 
      10-19-2006
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!

 
Reply With Quote
 
 
 
 
Richard Tobin
Guest
Posts: n/a
 
      10-19-2006
In article <(E-Mail Removed) .com>,
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
p.lepin@ctncorp.com
Guest
Posts: n/a
 
      10-20-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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
xmlnssl="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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
So you think PythonCard is old? Here's new wine in an old bottle. John Henry Python 24 05-30-2008 12:00 AM
ATI: Recycling Old Cards With New Names Silverstrand Front Page News 2 08-29-2006 03:02 PM
new hard disk on old, old pc Deano Computer Support 5 10-21-2005 12:32 PM
Possible to traverse the children of an element without specifying child element names? Matt XML 2 10-12-2004 08:23 AM
Matching attribute names to element names in a different path Carl XML 0 04-01-2004 01:15 PM



Advertisments