Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 9th, 2006, 09:13 PM
Registered User
 
Join Date: Oct 2006
Location: , , .
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.


Reply With Quote
  #2 (permalink)  
Old October 10th, 2006, 02:22 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old October 10th, 2006, 03:06 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
  #4 (permalink)  
Old October 10th, 2006, 07:10 AM
Registered User
 
Join Date: Oct 2006
Location: , , .
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.

Reply With Quote
  #5 (permalink)  
Old October 10th, 2006, 07:20 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:09 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.