473,408 Members | 1,822 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,408 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 2071
"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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.