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 | 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 July 17th, 2008, 06:32 PM
Authorized User
 
Join Date: Jul 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default simple question

I would like to determine whether a element C with a selected attribute 'car' is the first element within <A>,but one point is very import , something like this Structure can appear in font of this.In other way there is no element which satisfies ancestor::A and preceding::C.Thanks a lot.
<A>
  <B>
    <C name="car"> </C>
   </B>
     <C name="car"> </C>
    <D> </D>
</A>


Reply With Quote
  #2 (permalink)  
Old July 18th, 2008, 03:16 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Sorry, but your question isn't simple at all!

>I would like to determine whether a element C with a selected attribute 'car' is the first element within <A>

Do you mean

(1) "is the first child element of <A>"

(2) "is the first child element of <A> whose name is <C>"

(3) "is the first child element of <A> that has a selected attribute whose name is 'name' and whose value is 'car'

(4) "is the first descendant element of <A>"

(5) "is the first descendant element of <A> whose name is <C>"

(6) something else....?

>something like this Structure can appear in font of this

What does "this Structure" refer to?

(1) The whole of the XML fragment you have shown?

(2) The <B> element?

(3) Something else?

>In other way there is no element which satisfies ancestor::A and preceding::C

In your example, that condition is satisfied by the first C, and is not satisfied by the second C. But until now, I thought your rules would match the second C but not the first. Please be more clear!



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #3 (permalink)  
Old July 18th, 2008, 03:22 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
   <xsl:template match="A">
      <xsl:choose>
         <xsl:when test="generate-id(//C[1])=generate-id(//C[@name='car'][1])">the first C has name "car"</xsl:when>
         <xsl:otherwise>the first C's name is not "car"</xsl:otherwise>
      </xsl:choose>
   </xsl:template>
Reply With Quote
  #4 (permalink)  
Old July 18th, 2008, 03:37 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

@Volder: Two points about your example above - firstly, the "//C" will mean the the 'C' element wouldn't have to be the child of 'A'. Secondly I believe that ".//C[1][@name='car']" will have the same effect as the above code, and may be faster.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #5 (permalink)  
Old July 18th, 2008, 03:52 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: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Looking at Volder's reply:

  <xsl:template match="A">
      <xsl:choose>
         <xsl:when test="generate-id(//C[1])=generate-id(//C[@name='car'][1])">the first C has name "car"</xsl:when>
         <xsl:otherwise>the first C's name is not "car"</xsl:otherwise>
      </xsl:choose>
   </xsl:template>

(1) I suspect that "//C" (both occurrences) was supposed to be ".//C"; I also suspect that ".//C[1]" should be "(.//C)[1]" or "descendant::C[1]" - the first descendant C, as distict from every C descendant that is the first child of its parent.

(2) If the OP really meant to ask the question "Is the name attribute of the first C descendant equal to 'car'?" (and it's not at all clear to me that this was the intent), then the question can be answered with the rather simpler expression

test="descendant::C[1]/@name = 'car'"

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #6 (permalink)  
Old July 18th, 2008, 04:19 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

>test="descendant::C[1]/@name = 'car'"

yep, this is what I meant
and accept my mistakes.

Reply With Quote
  #7 (permalink)  
Old July 18th, 2008, 04:36 AM
Authorized User
 
Join Date: Jul 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

dear All,
my purpose is making xslt intelligent as human being has ,i give quantification 'a' in front of the first appearance of 'Car',and give 'the' to the following 'car' .i simplify this code in order to be be understood easily,but this code is more complex and this is just a piece of whole code which is comprised of many units having <A> element. one thing we should care about fore unit is that there is possibility that a element with attribute whose value 'car' .i want to make sure in this unit the 'car' is the first 'car'.so xslt parses <C> of every unit,when it find that it is ,then add 'a' ,otherwise 'the' .i hope that i make the thing clear.what i have to mention is that our template is on every <C>

Reply With Quote
  #8 (permalink)  
Old July 18th, 2008, 04:55 AM
Authorized User
 
Join Date: Jul 2008
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it works using function generate-id().thanks to everyone.

Reply With Quote
  #9 (permalink)  
Old July 18th, 2008, 07:57 AM
Authorized User
 
Join Date: May 2008
Location: Moscow, , Russia.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by petergoodman
 dear All,
my purpose is making xslt intelligent as human being has ,i give quantification 'a' in front of the first appearance of 'Car',and give 'the' to the following 'car' .i simplify this code in order to be be understood easily,but this code is more complex and this is just a piece of whole code which is comprised of many units having <A> element. one thing we should care about fore unit is that there is possibility that a element with attribute whose value 'car' .i want to make sure in this unit the 'car' is the first 'car'.so xslt parses <C> of every unit,when it find that it is ,then add 'a' ,otherwise 'the' .i hope that i make the thing clear.what i have to mention is that our template is on every <C>
if I were a developer and someone wrote requirments sepcification in that a manner - I'd better crash my head over the wall )))

petergoodman, please read it yourself, just to see whether you can understand it yourself?

Quote:
quote:Originally posted by petergoodmanit works using function generate-id().thanks to everyone.
if you were attentive reading the thread you would realize, that
1) it is not totally correct
2) there is much simpler version to do the same

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
Very Simple Question YoungLuke C# 2 May 4th, 2007 02:23 AM
Simple Question ironchef Java GUI 0 September 14th, 2006 04:56 PM
Simple Question dpkbahuguna Java Basics 2 May 19th, 2006 12:05 AM
Simple question happyheart_man Pro VB.NET 2002/2003 0 January 14th, 2004 11:38 AM



All times are GMT -4. The time now is 12:08 PM.


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