473,387 Members | 1,504 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,387 software developers and data experts.

XSLT parsing

Hi,

I have an XML file created by a third party in which an element with a
simple content model has a text value consisting of 2 parts separated by a
colon, like this

<link>machine:port</link>

Is XSLT capable of parsing the value of a <link> element, to separately
extract the portions before and after the colon?

Regards
David Walker
Jul 20 '05 #1
3 4117


David Walker wrote:

I have an XML file created by a third party in which an element with a
simple content model has a text value consisting of 2 parts separated by a
colon, like this

<link>machine:port</link>

Is XSLT capable of parsing the value of a <link> element, to separately
extract the portions before and after the colon?


Yes, XPath 1.0 is used in XSLT 1.0 and has functions to break up strings
e.g.
substring-before(//link, ':')
substring-after(//link, ':')

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
*David Walker* wrote:
I have an XML file created by a third party in which an element with a
simple content model has a text value consisting of 2 parts separated
by a colon, like this

<link>machine:port</link>

Is XSLT capable of parsing the value of a <link> element, to
separately extract the portions before and after the colon?


Yes, e.g.:

<xsl:template match="link">
<xsl:choose>
<xsl:when test="contains(., ':')">
<xsl:text>Before = &quot;</xsl:text>
<xsl:value-of select="substring-before(., ':')"/>
<xsl:text>&quot;, after = &quot;</xsl:text>
<xsl:value-of select="substring-after(., ':')"/>
<xsl:text>&quot;</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

--
Andrew Urquhart
- CIWAS CSS FAQ: www.css.nu/faq/ciwas-aFAQ.html
- CIWAS Archive: www.tinyurl.com/ysjbm (Google Groups)
- My reply address is invalid, use: www.andrewu.co.uk/contact/
Jul 20 '05 #3
"David Walker" <da***@cs.cf.ac.uk> wrote in message news:<ca**********@news.swman.net.uk>...
Hi,

I have an XML file created by a third party in which an element with a
simple content model has a text value consisting of 2 parts separated by a
colon, like this

<link>machine:port</link>

Is XSLT capable of parsing the value of a <link> element, to separately
extract the portions before and after the colon?

Regards
David Walker


David,

I suppose there are many ways of achieving that. Here is just one example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="link"/>
</xsl:template>
<xsl:template match="link">
<xsl:variable name="machine" select="substring-before( . , ':' ) "/>
<xsl:variable name="port" select="substring-after( . , ':' ) "/>
<xsl:element name="link">
<xsl:element name="machine">
<xsl:value-of select="$machine"/>
</xsl:element>
<xsl:element name="port">
<xsl:value-of select="$port"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

This will transform Your example

<?xml version="1.0" encoding="UTF-8"?>
<link>machine:port</link>

To the following:

<?xml version="1.0" encoding="UTF-8"?>
<link>
<machine>machine</machine>
<port>port</port>
</link>

Is this something like You were after?

Rgds,

<kimmo/>
Jul 20 '05 #4

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

Similar topics

9
by: Hayko Riemenschneider | last post by:
Hi! I've got me an XSL tranformation stylesheet for my XML file. In the XSL file I now wish to use PHP to do some scripting. So I thought I'll use the PIs like this: ...
5
by: K. N. | last post by:
Is there any good and fast Python module for XSLT processing ? I'm going to use XML and XSLT to generate web pages, so I need XSLT processor that will be able to transform for example a DOM object...
12
by: gipsy boy | last post by:
Hello, I have sort of a big problem. I would really appreciate any help you could give me. I made a web service in C++ that throws XML to the client (browser). But, the XSLT transormation...
6
by: David Walker | last post by:
Hi, I have an XML file created by a third party in which an element with a simple content model has a text value consisting of 2 parts separated by a colon, like this ...
3
by: crc128 | last post by:
Hi, I'm looking at using XML and XSLT with apache cocoon to automatically generate html files for a site. the problem I'm having is that I want one of the pages to display a google map, but the...
2
by: veracon | last post by:
Hello, I'm looking to use XML and XSLT for templates in a system I'm writing, however I'm not really sure which parser is the "best". Basically, which library has the most features, and which is...
9
by: daph4ntom | last post by:
Right, I need some some views on the pros and cons of using XSLT versus ASP.NET 2.0 for the Presentation layer of an app. My company are looking at creating multiple sites and multi lingual...
12
by: Chris | last post by:
Hi, Just wondering if anyone out there knows if it is possible to convert a CSV to xml using XSLT? I've seen a lot of examples of xml to CSV, but is it possible to go back the other way? I...
4
by: mark4asp | last post by:
I have an element, report which contains tags which have been transformed. E.g. <pis &lt;p&gt <myXml> <report>This text has html tags in it.&lt;p&gt which but <has been changed to &lt;&gt</report>...
2
by: astroboiii | last post by:
New to the whole xml thing and finding w3schools to be an excellent resource. Now down to my question: I have several xml files I need to parse through and grab relevant information from and...
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
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
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...

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.