473,809 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>JO HN SMITH@19495 BISCAYNE
BLVD@MIAMI@FL/33180@USA</FullAddress>

The Result should be:

<Name>JOHN SMITH</Name>
<Address>1949 5 BISCAYNE BLVD</Address>
<City>MIAMI</City>
<State>FL</State>
<ZipCode>3318 0</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>19 495 BISCAYNE BLVD@MIAMI@FL/33180@USA</FullAddress>

Any Ideas?
Thanks
Nov 12 '05 #1
4 2105
"Alexis" <is******@yahoo .com> wrote in message news:b2******** *************** ***@posting.goo gle.com...
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:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">s treet,city,regi on,country</xsl:param>

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

<xsl:if test="substring-before($scheme, ',')">
<xsl:element name="{substrin g-before($scheme, ',')}">
<xsl:value-of select="substri ng-before($encoded Arg,'@')" />
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substri ng-after($scheme,' ,')" />
<xsl:with-param name="encodedAr g" select="substri ng-after($encodedA rg,'@')" />
</xsl:call-template>
</xsl:if>
</xsl:template>

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

<xsl:template match="FullAddr ess">
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="concat( $scheme,',')" />
<xsl:with-param name="encodedAr g" 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>JO HN SMITH@19495 BISCAYNE
BLVD@MIAMI@FL/33180@USA</FullAddress>
2 - <FullAddress>19 495 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******* ******@TK2MSFTN GP15.phx.gbl>.. .
"Alexis" <is******@yahoo .com> wrote in message news:b2******** *************** ***@posting.goo gle.com...
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:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">s treet,city,regi on,country</xsl:param>

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

<xsl:if test="substring-before($scheme, ',')">
<xsl:element name="{substrin g-before($scheme, ',')}">
<xsl:value-of select="substri ng-before($encoded Arg,'@')" />
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substri ng-after($scheme,' ,')" />
<xsl:with-param name="encodedAr g" select="substri ng-after($encodedA rg,'@')" />
</xsl:call-template>
</xsl:if>
</xsl:template>

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

<xsl:template match="FullAddr ess">
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="concat( $scheme,',')" />
<xsl:with-param name="encodedAr g" 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******* ******@TK2MSFTN GP15.phx.gbl>.. .
"Alexis" <is******@yahoo .com> wrote in message news:b2******** *************** ***@posting.goo gle.com...
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:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">s treet,city,regi on,country</xsl:param>

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

<xsl:if test="substring-before($scheme, ',')">
<xsl:element name="{substrin g-before($scheme, ',')}">
<xsl:value-of select="substri ng-before($encoded Arg,'@')" />
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substri ng-after($scheme,' ,')" />
<xsl:with-param name="encodedAr g" select="substri ng-after($encodedA rg,'@')" />
</xsl:call-template>
</xsl:if>
</xsl:template>

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

<xsl:template match="FullAddr ess">
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="concat( $scheme,',')" />
<xsl:with-param name="encodedAr g" 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.goo gle.com...
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,

- - - SplitsvilleTheS equel.xsl
<xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">

<xsl:param name="scheme">c ountry,region,c ity,street,name </xsl:param>

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

<xsl:if test="substring-before($encoded Arg,'@')"> <!-- Line 9 -->
<xsl:call-template name="split">
<xsl:with-param name="scheme" select="substri ng-after($scheme,' ,')" />
<xsl:with-param name="encodedAr g" select="substri ng-after($encodedA rg,'@')" />
</xsl:call-template>
<xsl:element name="{substrin g-before($scheme, ',')}">
<xsl:value-of select="substri ng-before($encoded Arg,'@')" />
</xsl:element>
</xsl:if>
</xsl:template>

<xsl:template name="reverse">
<xsl:param name="unreverse dArgs" />

<xsl:if test="substring-before($unrever sedArgs,'@')">
<xsl:call-template name="reverse">
<xsl:with-param name="unreverse dArgs" select="substri ng-after($unrevers edArgs,'@')" />
</xsl:call-template>
<xsl:value-of select="concat( substring-before($unrever sedArgs,'@'),'@ ')" />
</xsl:if>
</xsl:template>

<xsl:template match="FullAddr ess">

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

</xsl:template>

<xsl:template match="/">
<address>
<xsl:apply-templates select="FullAdd ress" />
</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($encoded Arg,'@')">

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
3525
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. - Creating training material themselves in many langauges built to order. The main thing I'm thinking about for the company is maybe a sort of content
12
6086
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 parameter to the xslt file. When I select a value from the drop-down list nothing appears. I want just a basic listing of Title, Image, and Description outputed. I have done a basic JavaScript that uses the .write() function and that worked so the...
4
1973
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 handle on how close XML is to replacing HTML for browsing specifically (I am aware of its other benefits.) Also I do understand that XML can be transformed into HTML but would rather not do this for various reasons.
5
7395
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 store and render web content. Reason being that we are considering migrating our website content to XML and using XSLT to transform the content into html. Currently lack of browser support for XSLT has held us back. For example a significant...
5
7644
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 business components that can be consumed by WebForms, WinForms, mobile devices, etc? Is it even fair to compare the such technologies? - How about for cases when you need to display dynamic elements on the form/grid (as compared to knowing data elements...
0
3410
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 please help me in converting this c# code to vb.net. I tried, but it is not giving the output. Here is the c# code using System using System.Collections using System.ComponentModel using System.Data using System.Drawing using System.Web using...
6
4996
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 exceptions with large XML files. I tried a simple punt of -Xmx512MB but that didn't work. In the future, the input XML file may become considerably bigger than 96 MB, so even if it did work, it probably would be putting off the inevitable to some later...
1
2745
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 consuming. I keep getting the following error when I submit my email to their site. System.Xml.Xsl.XslTransformException: Cannot find the script or external object that implements prefix 'ext:MailingList'. at...
4
2144
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 = "yes" /> <xsl:attribute-set name = "set1" > <xsl:attribute name = "a" >1</xsl:attribute> <xsl:attribute name = "b" >2</xsl:attribute> </xsl:attribute-set> <xsl:attribute-set name = "set2" >
9
2507
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 use XslCompiledTransform. But all this was rendered using an Xml control and I can't find a way to relate this control to an XslCompiledTransform ? Any help appreciated to solve this migration pb.
0
9601
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10378
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7653
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4332
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 we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.