Non-XML tagged value text to XML

Discussion in 'XML' started by mikea_59, Jan 13, 2005.

  1. mikea_59

    mikea_59 Guest

    I would like to use XSLT to translate some tagged value text to XML
    elements like this:

    Input Doc:

    <data>x=1.234 y=ABC z="Hello World"</data>

    Output Doc:

    <x>1.234</value>
    <y>ABC</y>
    <z>&quot;Hello World&quot;</z>

    Is XSLT up to the task? What would it look like? Most of the XSLT
    string processing code I've seen looks very verbose - can't be too
    efficient.
    mikea_59, Jan 13, 2005
    #1
    1. Advertising

  2. mikea_59 wrote:

    > I would like to use XSLT to translate some tagged value text to XML
    > elements like this:
    >
    > Input Doc:
    >
    > <data>x=1.234 y=ABC z="Hello World"</data>
    >
    > Output Doc:
    >
    > <x>1.234</value>
    > <y>ABC</y>
    > <z>&quot;Hello World&quot;</z>
    >
    > Is XSLT up to the task? What would it look like? Most of the XSLT
    > string processing code I've seen looks very verbose - can't be too
    > efficient.


    Using XSLT 2.0 you can do that with regular expression matching as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <xsl:eek:utput method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
    <results>
    <xsl:apply-templates />
    </results>
    </xsl:template>

    <xsl:template match="data">
    <xsl:analyze-string select="." regex="((\w+)=(&quot;.*&quot;|\S+))">
    <xsl:matching-substring>
    <xsl:element name="{regex-group(2)}">
    <xsl:value-of select="regex-group(3)" />
    </xsl:element>
    </xsl:matching-substring>
    </xsl:analyze-string>
    </xsl:template>

    </xsl:stylesheet>

    Output with Saxon 8.2 is

    <?xml version="1.0" encoding="UTF-8"?>
    <results>
    <x>1.234</x>
    <y>ABC</y>
    <z>"Hello World"</z>
    </results>

    With XSLT 1.0/XPath 2.0 you need to write a template that processes the
    string x=1.234 y=ABC z="Hello World" recursively using functions
    substring-before/substring-after.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 13, 2005
    #2
    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. Sudsy
    Replies:
    1
    Views:
    332
    Brad BARCLAY
    Jan 7, 2004
  2. RobM
    Replies:
    1
    Views:
    358
  3. Ranga

    scope and life of tagged structure

    Ranga, Mar 17, 2005, in forum: C Programming
    Replies:
    4
    Views:
    350
    CBFalconer
    Mar 20, 2005
  4. Johan Tibell

    Tagged unions

    Johan Tibell, Jul 21, 2006, in forum: C Programming
    Replies:
    7
    Views:
    391
    Chris Torek
    Jul 22, 2006
  5. metal
    Replies:
    2
    Views:
    284
    Kay Schluehr
    Dec 12, 2009
Loading...

Share This Page