Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 17th, 2010, 04:03 PM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default Converting delimited text to separate XML Elements

I've purchased Michael Kay's XSLT 2.0 and XPATH 2.0 (4th ed.) as a raw beginning with XSLT, and I'm trying to learn as I go along. I've done ok till now.

Yes I'm using XSLT 2.0

I'm actually trying to create an export filter for OpenOffice.org to create a custom XML document for another bit of software I use regularly. And I'm nearly there! But now I've hit a snag as I'm teaching myself through this process.

I am attempting to transfer a semicolon delimited element in one XML file to a new one.
Essentially I'm trying to go from:
Document A's meta data which contains:
Code:
<meta:keyword>Anna; Simeon; Character; Temple</meta:keyword>
into Document B which has the form:
Code:
<topics>
    <topic></topic>
</topics>
The result actually needs to look like
Code:
<topics>
    <topic>Anna</topic>
    <topic>Simeon</topic>
    <topic>Character</topic>
    <topic>Temple</topic>
</topics>


I'm staring at the tokenize function (pp 894-ff) and thinking that it must be the answer, but I'm just plain lost. I've also tried to follow this thread:
XML element with delimited values and XSLT, but once again I'm stuck. Probably because I'm such a noob at this.

Can anyone graciously assist my learning at this point?

Thanks in advance for your consideration.
 
Old March 17th, 2010, 06:30 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Assuming all your namespaces etc are sorted out then the following should work:

Code:
<xsl:template match="meta:keyword">
  <topics>
    <xsl:for-each select="tokenize(., ';')">
      <topic><xsl:value-of select="normalize-space(.)"/></topic>
    </xsl:for-each>
  </topics>
</xsl:template>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
tcblack (March 18th, 2010)
 
Old March 17th, 2010, 07:12 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

And another approach is

Code:
<xsl:template match="meta:keyword">
  <topics>
    <xsl:analyze-string select="." regex=";\s+">
      <xsl:matching-substring>
        <topic><xsl:value-of select="normalize-space(.)"/></topic>
      </xsl:matching-substring>
    </xsl:analyze-string>
  </topics>
</xsl:template>
As you see, tokenize() is probably easier here, but xsl:analyze-string can be useful when things get more complicated, for example when you need to retain the delimiters.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
tcblack (March 18th, 2010)
 
Old March 18th, 2010, 11:39 AM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default Thank you Both!

Thank you both for answering.

As Sam suggested, I'm having an issue with namespaces.

I'm stuck on getting the stylesheet to look at the right document for this particular transform.

Normally if I was using xsl:value-of I could pull the whole path to the document as:
Code:
<xsl:value-of select="/office:document/office:meta/meta:keyword />
This is what I do throughout the transformation sheet.

I'm working against about four XML documents in order to pull the information I need into the new one.
I tried to modify the suggested code with that full path, but alas it fails.
Code:
<xsl:template match="/office:document/office:meta/meta:keyword">
 
Old March 18th, 2010, 12:14 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

We need to see more context to tell what is wrong such as the input XML document(s) and the other code of the stylesheet that pulls in the document(s) for processing and applies templates. An XSLT stylesheet can have one primary input document, any other document(s) you need to pull in using the "document" function (XSLT 1.0 and 2.0) or the "doc" function (XSLT/XPath 2.0). And then you will have use apply-templates to select nodes for further processing, if you want your template to be applied.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old March 18th, 2010, 06:02 PM
Registered User
 
Join Date: Mar 2010
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Martin,
Thank you... however I was able to figure it out!
I really appreciate you all for helping me out.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Wiki-like text into XML igraham XSLT 7 February 28th, 2008 06:18 PM
converting text files to xml anandthecoolest Visual Studio 2005 1 March 8th, 2007 03:24 PM
Tags as text in XML elements janise XML 16 January 2nd, 2007 06:45 AM
Importing Delimited Text sdilucca Access 1 February 23rd, 2006 02:20 PM
Comma delimited text files bmurrin Beginning VB 6 8 February 26th, 2004 02:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.