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 July 17th, 2008, 06:32 PM
Authorized User
 
Join Date: Jul 2008
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>


 
Old July 18th, 2008, 03:16 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old July 18th, 2008, 03:22 AM
Authorized User
 
Join Date: May 2008
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>
 
Old July 18th, 2008, 03:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old July 18th, 2008, 03:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old July 18th, 2008, 04:19 AM
Authorized User
 
Join Date: May 2008
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.

 
Old July 18th, 2008, 04:36 AM
Authorized User
 
Join Date: Jul 2008
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>

 
Old July 18th, 2008, 04:55 AM
Authorized User
 
Join Date: Jul 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old July 18th, 2008, 07:57 AM
Authorized User
 
Join Date: May 2008
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






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





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