Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Non-XML tagged value text to XML

Reply
Thread Tools

Non-XML tagged value text to XML

 
 
mikea_59
Guest
Posts: n/a
 
      01-13-2005
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.

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-13-2005


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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
version="2.0">

<xslutput 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VLAN1 tagged on trunk? Steffen M. Steck Cisco 2 04-20-2006 05:57 PM
tagged ethernetframes over dsl (c870) arne Cisco 0 01-24-2006 02:30 PM
Many 802.1q tagged vlans, 1 Cisco 2621 router justin.krejci@gmail.com Cisco 3 12-05-2005 09:46 PM
Why tagged EIGRP routes arent propagated? Frozer Cisco 4 08-09-2005 11:17 AM
OT: Linux fans tagged with new label Sudsy Java 1 01-07-2004 02:36 PM



Advertisments