Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts

    XPath select first child element

    How would I select the first child element of an element. For example:
    Code:
    <parent>
     <child1>x</child1>
     <child2>y</child2>
     <child3>z</child3>
    </parent>
    I want to select <child1> but I don't know what it is.
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazzo View Post
    How would I select the first child element of an element. For example:
    Code:
    <parent>
     <child1>x</child1>
     <child2>y</child2>
     <child3>z</child3>
    </parent>
    I want to select <child1> but I don't know what it is.
    this could work:
    Code:
    parent/*[1]
    a condition must be inside of '[' and ']' and could use some function like position(), for example:
    Code:
    parent/*[psotion() = 1] is same as parent/*[1]
    parent/*[last]
    parent/*[name() = 'child2']
    see the xpath tutorial from the link I post in one of my answer to your posts.

    best regards

  • #3
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    That's what I tried but the problem is, it's within a for-each element. Like this:

    <xsl:for-each select="parent">
    <xsl:value-of select="The first child element of 'parent.'"/>

    How would I do that?
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazzo View Post
    That's what I tried but the problem is, it's within a for-each element. Like this:

    <xsl:for-each select="parent">
    <xsl:value-of select="The first child element of 'parent.'"/>

    How would I do that?
    few way to do same thing:
    1.)
    Code:
    <xsl:for-each select="parent">
       <xsl:value-of select="*[1]"/>
    </xsl:for-each>
    2.)
    Code:
    <xsl:for-each select="parent">
       <xsl:if test="position() = 1">
          </xsl:value-of select="*"/>
       </xsl:if>
    </xsl:for-each>
    3.)
    Code:
    <xsl:apply-template select="parent/*[1]" mode="mymode"/>
    
    <xsl:template match="*" mode="mymode">
       <xsl:value-of select="*"/>
    </xsl:template>
    I prefere 3.)

    best regards

  • Users who have thanked oesxyl for this post:

    Jazzo (11-02-2008)

  • #5
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    It works, thank you
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazzo View Post
    It works, thank you
    you are welcome, always,

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •