473,465 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

howto map/rename element names in xslt

I am trying to map external xml documents to a class in dotnet.
The problem is that the elements of the input xml will have different
names than the properties of the class.
How do I create the xslt that will allow me to transform the inbound
xml into another xml resource with the elements renamed so that I can
serialize it to an instance of the class.

I assume that this is best done with xslt.transform, but I am not sure
how to write the xslt.

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Customer>
<nameFirst>Joe</nameFirst>
<nameLast>Schmoe</nameLast>
<addrCity>Bigtime</addrCity>
<addrState>NY</addrState>
<addrStreet>100 E Main Street</addrStreet>
<addrZIP>100014</addrZIP>
<otherfield>25</otherfield>
</Customer>
</NewDataSet>

public class Cust
{
public Cust()
{ }
public string FirstName;
public string LastName;
public string City;
public string State;
public string Zip;
}
Thanks,
Pint
Nov 12 '05 #1
2 6761

Start with the root node, and progressively apply XSLT template rules
for each successive level of depth. The select and match attribute
values you will use will refer to element names from the original
source document. Conversely, the literal content that your tem-
plates emit into the target document will refer to the renamed
elements.

- - - Rename.xsl (excerpt)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />
<xsl:template match="/" >
<xsl:apply-templates select="./NewDataSet" />
</xsl:template>
<xsl:template match="NewDataSet" >
<xsl:apply-templates select="./Customer" />
</xsl:template>
<xsl:template match="Customer">
<!-- Observe, the template @match value is the source element name,
the literal element content below is the renamed element name. -->
<Cust>
<!-- Observe, the apply-templates (as in the corresponding template
@match below) refers to the source element name. -->
<xsl:apply-templates select="nameFirst" />
<!-- An xsl:apply-templates for each of the other child elements of
Customer in the source document would appear here, left as an
exercise for the reader. -->
</Cust>
</xsl:template>
<xsl:template match="nameFirst">
<FirstName>
<xsl:value-of select="text()" />
</FirstName>
</xsl:template>
<!-- The remaining template rules are left as an exercise for the reader. -->
- - -

Precise matching of the XmlSerializer output would also depend upon
any System.Xml.Serialization namespace attributes (metadata) that
have been associated with the C# class. These weren't shown, so
the above transform presumes elements with simple text content.
Derek Harmon
Nov 12 '05 #2
>Start with the root node, and progressively apply XSLT template rules
for each successive level of depth. The select and match attribute
values you will use will refer to element names from the original
source document. Conversely, the literal content that your tem-
plates emit into the target document will refer to the renamed
elements.

- - - Rename.xsl (excerpt)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />
<xsl:template match="/" >
<xsl:apply-templates select="./NewDataSet" />
</xsl:template>
<xsl:template match="NewDataSet" >
<xsl:apply-templates select="./Customer" />
</xsl:template>
<xsl:template match="Customer">
<!-- Observe, the template @match value is the source element name,
the literal element content below is the renamed element name. -->
<Cust>
<!-- Observe, the apply-templates (as in the corresponding template
@match below) refers to the source element name. -->
<xsl:apply-templates select="nameFirst" />
<!-- An xsl:apply-templates for each of the other child elements of
Customer in the source document would appear here, left as an
exercise for the reader. -->
</Cust>
</xsl:template>
<xsl:template match="nameFirst">
<FirstName>
<xsl:value-of select="text()" />
</FirstName>
</xsl:template>
<!-- The remaining template rules are left as an exercise for the reader. -->
- - -

Precise matching of the XmlSerializer output would also depend upon
any System.Xml.Serialization namespace attributes (metadata) that
have been associated with the C# class. These weren't shown, so
the above transform presumes elements with simple text content.
Derek Harmon


thanks. That was a great help.

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ray Tayek | last post by:
hi, trying to use an xslt to make an xslt. trying something like: <?xml version="1.0" encoding="UTF-8"?> <?xmlspysamplexml H:\java\projects\spy1\spy\inputDocumentMap.xml?> <xsl:stylesheet...
0
by: Carl | last post by:
I want to create a generic xslt that would take xml input like: ########################################################################## <?xml version="1.0" encoding="utf-8" ?>...
2
by: Matt | last post by:
Is it possible to traverse the values of children of an element, and no need to specify the children's element names in XSLT element? The XML file can be <bio> <skills> <languages>...
2
by: Jordan Willms | last post by:
Hi there. I am working with lom metadata and I am a little confused with how to form the following xml element: <lom xmlns="http://www.imsglobal.org/xsd/imsmd_v1p2"...
4
by: ina | last post by:
Hello all, I am newbie in xml and have a problem with this parse. I have this xml.file <Style> <Strategy>
8
by: bennett.matthew | last post by:
Hello all, This is probably an elementary (no pun intended) question, but I've spent all afternoon on it and it's driving me crazy. I have a function which dynamically adds to a table. It...
8
by: Scott Sauyet | last post by:
I found myself needing to find my way recursively through a document in XSLT, finding the dependencies one element had on others, and including only those in an initial set, their dependencies, the...
2
by: =?iso-8859-1?b?cultaQ==?= | last post by:
Hi, I would like to rename files (jpg's ones) using a text file containing the new names... Below is the code that doesn't work : ***** #!/usr/bin/python #-*- coding: utf-8 -*- from os...
6
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.