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 January 17th, 2007, 04:01 PM
Registered User
 
Join Date: Jan 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help with matching multiple "begins-wth"

I am a relative noob when it comes to XSLT and I need some help with a match expression. I need to match a set (any length) of elements that all begin with the same text in the node name. Example:

<person>
   <phone1AC />
   <phone1Exc />
   <phone1Num />

   <phone2AC />
   <phone2Exc />
   <phone2Num />

   <lastName />
   <firstName />
   <etc />
</person>

I need a match expression which matches the 3 'phone1' nodes and then (again) the 3 'phone2' nodes but not the 'lastName', 'firstName' or 'etc' nodes. I _CAN_ know the prefixes ('phone1', 'phone2') in advance, but it would be even better if the match expression matched arbitrary begins-with strings.... is that too much to ask?

Thanks very much in advance,

=dave=
 
Old January 17th, 2007, 05:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Your terminology isn't quite right, unfortunately, which means I might be misunderstanding your problem. There are two separate things in XSLT: a pattern, also called a match pattern, which is used in the "match" attribute of xsl:template, and an expression, also called an XPath expression or select expression, used for example in the "select" attribute of instructions like xsl:apply-templates and xsl:value-of. I'm not sure which of these you mean by "match expression".

It's easy enough to match on a fixed prefix, for example

<xsl:template match="*[starts-with(name(), 'phone']">

or

<xsl:for-each select="*[starts-with(name(), 'phone']">

In a select expression you can also use a variable:

<xsl:for-each select="*[starts-with(name(), $prefix]">

In XSLT 2.0 you can also do this in a match pattern:

<xsl:template match="*[starts-with(name(), $prefix]">

However, XSLT 1.0 doesn't allow variables in match patterns.

A general point about your XML design: I think it's trying to pack too much information into the names of the elements. It would be much cleaner to use a hierarchic structure:

<person>
   <phone nr="1">
      <AC/>
      <Exc />
      <Num />
   </phone>
   <phone nr="2">
      <AC/>
      <Exc />
      <Num />
   </phone>
   <lastName />
   <firstName />


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old January 17th, 2007, 10:53 PM
Registered User
 
Join Date: Jan 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Michael, you have sent me in a couple of directions that seem promising.

Your suggestion to change the structure of the input is a good one....sadly, I cannot change the DTD of the input XML.

You have confused me a bit. I was under the impression that an "XPath expression" could be used as a "match pattern." I never gave it much thought, but I guess I also thought the reverse was true. In your example:

<xsl:template match="*[starts-with(name(), 'phone']">

or

<xsl:for-each select="*[starts-with(name(), 'phone']">

The two look remarkably similar, to me. Can you point me toward a resource that explains the differences (you mentioned one difference in your reply... there are others?)?

I had it in my head that I was looking for a "match pattern" because I thought I wanted to use templates. My current thought is that using a "choose" with an "xpath expression" is probably a better way to go, in this particular case.

Thanks very much for your prompt (and useful) response.

=dave=
 
Old January 18th, 2007, 04:32 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>Can you point me toward a resource that explains the differences

(a) the spec (search for XSLT 1.0 Recommendation or XSLT 2.0 Recommendation)

(b) my book (since we're on a Wrox site).

Match patterns are essentially a subset of XPath expressions.



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
can anybody share ur knowledge wth ajax kunchala_rakesh Ajax 1 September 13th, 2007 10:10 PM
Crystal Reports wth Java anandh J2EE 0 February 28th, 2007 09:13 AM
textbox names wth numbers popweasel VB How-To 1 January 3rd, 2006 06:50 PM
Bookpool.com Promotion begins today jbergman Wrox Book Feedback 0 August 1st, 2003 09:34 AM
File Download Begins.....why? tenikiwon Beginning PHP 0 June 12th, 2003 10:45 AM





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