XSLT parsing

Discussion in 'XML' started by David Walker, Jun 18, 2004.

  1. David Walker

    David Walker Guest

    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 Walker, Jun 18, 2004
    #1
    1. Advertising

  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, 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/
    Martin Honnen, Jun 18, 2004
    #2
    1. Advertising

  3. *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:eek:therwise>
    <xsl:value-of select="."/>
    </xsl:eek:therwise>
    </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/
    Andrew Urquhart, Jun 18, 2004
    #3
  4. "David Walker" <> wrote in message news:<caug5e$61h$>...
    > 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/>
    Kimmo J?rvikangas, Jun 18, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    658
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,680
    Dimitre Novatchev
    Sep 25, 2003
  3. ted
    Replies:
    1
    Views:
    622
    Laurens
    Jan 26, 2004
  4. Replies:
    2
    Views:
    719
    Henry S. Thompson
    Oct 19, 2005
  5. Replies:
    4
    Views:
    670
Loading...

Share This Page