473,378 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

XML to XML Using XSLT

Hi,
I need to transform one xml document into a second xml document. They
both have many nodes so xslt works fine, but there is one node I have
not figure out how to transform.

Here it is:
Node in Input File.

<FullAddress>JOHN SMITH@19495 BISCAYNE
BLVD@MIAMI@FL/33180@USA</FullAddress>

The Result should be:

<Name>JOHN SMITH</Name>
<Address>19495 BISCAYNE BLVD</Address>
<City>MIAMI</City>
<State>FL</State>
<ZipCode>33180</ZipCode>
<Country>USA</Country>

It seems to me I have to "Split" or "Parse" the FullAddress node by
the "@" character, but HOW I do that in XSLT?

Note: The Name element is optional in the FullAdress node. So this
input node is also valid.

<FullAddress>19495 BISCAYNE BLVD@MIAMI@FL/33180@USA</FullAddress>

Any Ideas?
Thanks
Nov 12 '05 #1
4 2068
"Alexis" <is******@yahoo.com> wrote in message news:b2**************************@posting.google.c om...
It seems to me I have to "Split" or "Parse" the FullAddress node by
the "@" character, but HOW I do that in XSLT?


As a generalized solution, I'd advise using recursion and provide the structure of your
target "schema" in a variable, as demonstrated in the following example which is similar
to your example (it doesn't take the extra step of splitting the zip code from it's state).

- - - Splitsville.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">street,city,region,country</xsl:param>

<xsl:template name="split">
<xsl:param name="scheme" />
<xsl:param name="encodedArg" />

<xsl:if test="substring-before($scheme,',')">
<xsl:element name="{substring-before($scheme,',')}">
<xsl:value-of select="substring-before($encodedArg,'@')" />
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substring-after($scheme,',')" />
<xsl:with-param name="encodedArg" select="substring-after($encodedArg,'@')" />
</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template match="/">
<address>
<xsl:apply-templates select="FullAddress" />
</address>
</xsl:template>

<xsl:template match="FullAddress">
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="concat($scheme,',')" />
<xsl:with-param name="encodedArg" select="concat(text(),'@')" />
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
- - -

The currently executing call to the named template: split, operates on the string(s) it's
passed for $scheme and $encodedArg using the XPath function, substring-before( )
to break-off the first chunk of these strings before the delimiter (comma and '@' in
this instance).

Then at the end, I pass the unprocessed remainders of $scheme and $encodedArg
to another call of this same template using the substring-after( ) function.

Recursion ends when substring-before( ) indicates an empty string (equivalent to false,
so the <xsl:if>'s test fails and the templates unwind).
Derek Harmon
Nov 12 '05 #2
Thanks Derek,
I really appreciate it. I review your solution and it is very good.
There is just one problem. The input xml may also contain a name
before the street address. As I said before I may get these two input
files:

1 - <FullAddress>JOHN SMITH@19495 BISCAYNE
BLVD@MIAMI@FL/33180@USA</FullAddress>
2 - <FullAddress>19495 BISCAYNE BLVD@MIAMI@FL/33180@USA</FullAddress>

Your solution fails with xml 1.
How can we test if name "JOHN SMITH" is in the input xml, so we can
"ignore" it?

Thanks again
/Alexis

"Derek Harmon" <lo*******@msn.com> wrote in message news:<uh*************@TK2MSFTNGP15.phx.gbl>...
"Alexis" <is******@yahoo.com> wrote in message news:b2**************************@posting.google.c om...
It seems to me I have to "Split" or "Parse" the FullAddress node by
the "@" character, but HOW I do that in XSLT?


As a generalized solution, I'd advise using recursion and provide the structure of your
target "schema" in a variable, as demonstrated in the following example which is similar
to your example (it doesn't take the extra step of splitting the zip code from it's state).

- - - Splitsville.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">street,city,region,country</xsl:param>

<xsl:template name="split">
<xsl:param name="scheme" />
<xsl:param name="encodedArg" />

<xsl:if test="substring-before($scheme,',')">
<xsl:element name="{substring-before($scheme,',')}">
<xsl:value-of select="substring-before($encodedArg,'@')" />
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substring-after($scheme,',')" />
<xsl:with-param name="encodedArg" select="substring-after($encodedArg,'@')" />
</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template match="/">
<address>
<xsl:apply-templates select="FullAddress" />
</address>
</xsl:template>

<xsl:template match="FullAddress">
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="concat($scheme,',')" />
<xsl:with-param name="encodedArg" select="concat(text(),'@')" />
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
- - -

The currently executing call to the named template: split, operates on the string(s) it's
passed for $scheme and $encodedArg using the XPath function, substring-before( )
to break-off the first chunk of these strings before the delimiter (comma and '@' in
this instance).

Then at the end, I pass the unprocessed remainders of $scheme and $encodedArg
to another call of this same template using the substring-after( ) function.

Recursion ends when substring-before( ) indicates an empty string (equivalent to false,
so the <xsl:if>'s test fails and the templates unwind).
Derek Harmon

Nov 12 '05 #3
Can you start from the end and move towards the begining.?

"Derek Harmon" <lo*******@msn.com> wrote in message news:<uh*************@TK2MSFTNGP15.phx.gbl>...
"Alexis" <is******@yahoo.com> wrote in message news:b2**************************@posting.google.c om...
It seems to me I have to "Split" or "Parse" the FullAddress node by
the "@" character, but HOW I do that in XSLT?


As a generalized solution, I'd advise using recursion and provide the structure of your
target "schema" in a variable, as demonstrated in the following example which is similar
to your example (it doesn't take the extra step of splitting the zip code from it's state).

- - - Splitsville.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">street,city,region,country</xsl:param>

<xsl:template name="split">
<xsl:param name="scheme" />
<xsl:param name="encodedArg" />

<xsl:if test="substring-before($scheme,',')">
<xsl:element name="{substring-before($scheme,',')}">
<xsl:value-of select="substring-before($encodedArg,'@')" />
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substring-after($scheme,',')" />
<xsl:with-param name="encodedArg" select="substring-after($encodedArg,'@')" />
</xsl:call-template>
</xsl:if>
</xsl:template>

<xsl:template match="/">
<address>
<xsl:apply-templates select="FullAddress" />
</address>
</xsl:template>

<xsl:template match="FullAddress">
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="concat($scheme,',')" />
<xsl:with-param name="encodedArg" select="concat(text(),'@')" />
</xsl:call-template>
</xsl:template>

</xsl:stylesheet>
- - -

The currently executing call to the named template: split, operates on the string(s) it's
passed for $scheme and $encodedArg using the XPath function, substring-before( )
to break-off the first chunk of these strings before the delimiter (comma and '@' in
this instance).

Then at the end, I pass the unprocessed remainders of $scheme and $encodedArg
to another call of this same template using the substring-after( ) function.

Recursion ends when substring-before( ) indicates an empty string (equivalent to false,
so the <xsl:if>'s test fails and the templates unwind).
Derek Harmon

Nov 12 '05 #4
"Alexis" <is******@yahoo.com> wrote in message news:b2**************************@posting.google.c om...
Can you start from the end and move towards the begining.?


Yes Alexis, in <xsl:template> rules you can do what's called
head recursion (where the 'work' is done after the recursive
call, that is, as the stack unwinds) or tail recursion (do
the 'work' first and then make a recursive call, which is
what the example I've shown demonstrated).

I used tail recursion because I don't believe I could find
a head recursive solution that's as efficient. An extra
processing step is required to 'reverse' the @-delimited
particles in your sample XML to affect a head recursive
solution. E.g.,

JOHN SMITH@17 BELLINGHAM DR@OWINGS MILLS@MD/21117@USA

needs to become,

USA@MD/21117@OWINGS MILLS@17 BELLINGHAM DR@JOHN SMITH

However, I understand you're asking for the approach so
that "optional" elements at the end can be omitted, and
it can be done more elegantly than testing for the num-
ber of particles or determining if the first particle
is a name or an address.

Here's a revised example,

- - - SplitsvilleTheSequel.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">country,region,city,street,name</xsl:param>

<xsl:template name="split">
<xsl:param name="scheme" />
<xsl:param name="encodedArg" />

<xsl:if test="substring-before($encodedArg,'@')"> <!-- Line 9 -->
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substring-after($scheme,',')" />
<xsl:with-param name="encodedArg" select="substring-after($encodedArg,'@')" />
</xsl:call-template>
<xsl:element name="{substring-before($scheme,',')}">
<xsl:value-of select="substring-before($encodedArg,'@')" />
</xsl:element>
</xsl:if>
</xsl:template>

<xsl:template name="reverse">
<xsl:param name="unreversedArgs" />

<xsl:if test="substring-before($unreversedArgs,'@')">
<xsl:call-template name="reverse">
<xsl:with-param name="unreversedArgs" select="substring-after($unreversedArgs,'@')" />
</xsl:call-template>
<xsl:value-of select="concat(substring-before($unreversedArgs,'@'),'@')" />
</xsl:if>
</xsl:template>

<xsl:template match="FullAddress">

<xsl:call-template name="split">
<xsl:with-param name="scheme" select="concat($scheme,',')" />
<xsl:with-param name="encodedArg">
<xsl:call-template name="reverse">
<xsl:with-param name="unreversedArgs" select="concat(text(),'@')" />
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>

</xsl:template>

<xsl:template match="/">
<address>
<xsl:apply-templates select="FullAddress" />
</address>
</xsl:template>

</xsl:stylesheet>
- - -

The reverse template reverses the particles between '@'
delimiters. (XPath 2.0 helps make this sort of operation
more direct, with it's new reverse() function, BTW.)

The outcome of reverse is what the split template receives
as it's $encodedArg. Notice that I reversed the $scheme
variable manually (you could also run a reverse template
on these, using a ',' delimiter instead of '@' which in
the general case could be a second parameter to reverse,
but I left out this for efficiency).

In the split template, all I did to make it head recursive
instead of tail recursive was swap the <xsl:call-template>
with the <xsl:element> that renders into the resulting
node set. This means we go all the way to the last particle
in $encodedArg and $scheme (which I want rendered first)
and then as the recursion unwinds that renders the first
element, and so on back up the stack.

I'll draw your attention to one other note about line 9,

<xsl:if test="substring-before($encodedArg,'@')">

has the effect of not emitting an optional element when
that text particle (name) was left unspecified. If,
however, you change this to,

<xsl:if test="substring-before($scheme,',')">

then you can emit an empty element for optional pieces
of information that were left unspecified.
Derek Harmon
Nov 12 '05 #5

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

Similar topics

12
by: Simon Harvey | last post by:
Whato chaps, I work (or am hoping to work! :) for a company that specialises in the following: - Localisation of media - including software, manuals, literature and training material. -...
12
by: Mark Constant | last post by:
I have a drop-down list now and I got it so when something is selected from the drop down list it calls a JavaScript function. I want it so the value selected from the drop-down list is sent as a...
4
by: Pat | last post by:
Is anyone using XML instead of HTML for pages of content? If so is there a public site to browse? Can someone point me to a discussion of the pros and cons of doing this? I am trying to get a...
5
by: Mike Judkins | last post by:
I'm looking for examples of websites (preferably large and well known companies, not personal sites or developer-to-developer sites) that use XML and XSLT as a technology platform from which to...
5
by: Fred | last post by:
Not much expertise on XSLT and trying to understand it's uses when creating apps in VS.NET? If I wanted flexibility on the UI (View aspect of M.V.C.): - How does it compare with creating...
0
by: canasdaq | last post by:
Can anyone please help me. I was looking at the article "http://www.eggheadcafe.com/articles/20030603.asp". I am new to .net and know nothing in c#. I want to write a menu in asp.net. Can anyone...
6
by: Lenny Wintfeld | last post by:
Hi I'm attempting additions/changes to a Java program that (among other things) uses XSLT to transform a large (96 Mb) XML file. It runs fine on small XML files but generates OutOfMemory...
1
by: Nick | last post by:
I am working on a website for a client and one of their requirements was to have a mailing list. I decided to XSLT to transform "templates" to HTML so that editing was very easy and less time...
4
by: simon.a.hulbert | last post by:
Hi, I'm trying to view the following xslt transformation using firefox <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" > <xsl:output method = "xml" indent =...
9
by: WT | last post by:
Hello, I have code created with .net 1.0 and migrated to 3.5. Form 2.0 the XslTransform class is obsolete and the vs2008 compiler generates warnings that these classes are absolete suggesting to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.