XSLT parsing

D

David Walker

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
 
M

Martin Honnen

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, ':')
 
A

Andrew Urquhart

*David Walker* said:
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:eek:therwise>
<xsl:value-of select="."/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
 
K

Kimmo J?rvikangas

David Walker said:
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/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top