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 October 9th, 2006, 09:13 PM
Registered User
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSL newbie question

Hi,

I'm a complete newbie to XSL and I was wondering if someone could help me with my first attempt of this technology.
Here's the XML:

<catalog xmlns="http://www.catalog.org/xml/export-0.3/">
  <dir>
    <id>user:XXX/home</id>
    <text>my text 1</text>
    <foo>zzz</foo>
  </dir>

  <dir>
    <id>user:XXX/home/work</id>
    <text>my text 2</text>
    <foo>zzz</foo>
  </dir>

  <dir>
    <id>user:YYY/home</id>
    <text>my text 3</text>
    <foo>zzz</foo>
  </dir>

  <dir>
    <id>Something completely different</id>
    <text>my text 4</text>
    <foo>zzz</foo>
  </dir>
</catalog>

I'd like to select the <dir> nodes who's <id> is something like "user:*/home". This is the expected output:

<new>
  <dir>
    <id>user:XXX/home</id>
    <text>my text 1</text>
  </dir>

  <dir>
    <id>user:YYY/home</id>
    <text>my text 3</text>
  </dir>
</new>

Thanks,
-S.


 
Old October 10th, 2006, 02:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Assuming you're using version 1.0 then I think you need to combine the starts-with() and substring-after() string functions. So a possible XPath would be:
Code:
/ns:catalog/ns:dir[starts-with(ns:id, 'user:') and substring-after(ns:id, '/') = 'home']
You will have to declare the ns prefix as bound to the default namespace. For links on this subject see http://p2p.wrox.com/topic.asp?TOPIC_ID=49630.

--

Joe (Microsoft MVP - XML)
 
Old October 10th, 2006, 03:06 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

XSLT 2.0 supports regular expressions, so you can write

<xsl:template match="catalog">
<new>
  <xsl:copy-of select="dir[matches(id, '^user:.*/work$')]"/>
</new>
</xsl:template>

Your example changes the namespace of all the elements, which the above transformation doesn't attempt: I wasn't sure that was an important part of your requirement or just something that happened by accident.

If you're constrained to use XSLT 1.0 it's a little more difficult because there are no regular expressions and no ends-with() function. You could change the predicate (the expression inside the square brackets) to:

starts-with(id, 'user:') and substring(id, string-length(id)-4) = '/work')

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old October 10th, 2006, 07:10 AM
Registered User
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I'm using Xalan-J and none of this is working :(
Any idea?

-S.

 
Old October 10th, 2006, 07:20 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If it's not working then you've done something wrong, and to tell you what you've done wrong, we'll need to see what you've done.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL(T) - NEWBIE Nabz XSLT 1 February 21st, 2008 04:36 AM
Newbie Question - Sorry! KP Crystal Reports 1 June 13th, 2006 02:45 AM
newbie in need of xsl help!! daula7 XSLT 1 May 12th, 2006 03:58 PM
Section Grouping (XSL Newbie!) vjlomas XSLT 1 February 4th, 2005 04:00 AM
Newbie question maccer88 Access 1 October 18th, 2003 05:53 PM





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