How to complete this test

Discussion in 'XML' started by mike, Feb 24, 2005.

  1. mike

    mike Guest

    I am have a problem with my xslt test.

    <data>
    <struct_id>34</struct_id>
    <par_struct_id>0</par_struct_id>
    <chd_struct_id>41</chd_struct_id>
    </data>
    ..
    ..
    ..

    <xsl:for-each select="root/data">
    <xsl:choose>
    <xsl:when test="par_struct_id = '0'">
    <xsl:if test="???">
    <xsl:element name="input"> <xsl:attribute
    name="type">text</xsl:attribute>
    </xsl:element>
    </xsl:if>
    </xsl:when>
    </xsl:choose>
    </xsl:for-each>

    In this example my xml has 3 occurrences where the par_struct_id = '0'
    and so I am getting output 3 times so that is cool.

    However, within that test I need to write out 1 occurrence of some
    element, an input element, but of course I get 3 elements.

    How do I limit the input to 1?
    mike, Feb 24, 2005
    #1
    1. Advertising

  2. mike

    Joris Gillis Guest

    Tempore 18:09:01, die Thursday 24 February 2005 AD, hinc in foro {comp.text.xml} scripsit mike <>:

    > <xsl:for-each select="root/data">
    > <xsl:choose>
    > <xsl:when test="par_struct_id = '0'">
    > <xsl:if test="???">
    > <xsl:element name="input"> <xsl:attribute
    > name="type">text</xsl:attribute>
    > </xsl:element>
    > </xsl:if>
    > </xsl:when>
    > </xsl:choose>
    > </xsl:for-each>
    >
    > In this example my xml has 3 occurrences where the par_struct_id = '0'
    > and so I am getting output 3 times so that is cool.
    >
    > However, within that test I need to write out 1 occurrence of some
    > element, an input element, but of course I get 3 elements.
    >
    > How do I limit the input to 1?


    Hi,

    If you would like the context node to be the first node (if any) that matches the requirements, then you can use something like:

    <xsl:for-each select="root/data[par_struct_id = '0'][1]">
    <xsl:element name="input">
    <xsl:attribute name="type">text</xsl:attribute>
    </xsl:element>
    </xsl:for-each>

    If the context node does not matter, write 'xsl:if' instead of 'xsl:for-each'.

    regards,
    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Η αλήθεια και το λάδι πάντα βγαίνουν από πάνω"
    Joris Gillis, Feb 24, 2005
    #2
    1. Advertising

  3. mike

    Joris Gillis Guest

    Tempore 18:57:51, die Thursday 24 February 2005 AD, hinc in foro {comp.text.xml} scripsit Joris Gillis <>:

    >> <xsl:for-each select="root/data">
    >> <xsl:choose>
    >> <xsl:when test="par_struct_id = '0'">
    >> <xsl:if test="???">
    >> <xsl:element name="input"> <xsl:attribute
    >> name="type">text</xsl:attribute>
    >> </xsl:element>
    >> </xsl:if>
    >> </xsl:when>
    >> </xsl:choose>
    >> </xsl:for-each>


    Replacing '???' with 'position()=1' will also work.

    --
    Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
    "Η αλήθεια και το λάδι πάντα βγαίνουν από πάνω"
    Joris Gillis, Feb 24, 2005
    #3
  4. mike

    mike Guest

    Thank you. that worked great
    mike, Feb 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sting
    Replies:
    5
    Views:
    1,610
    Natty Gur
    Jun 8, 2004
  2. Robert Allan Schwartz
    Replies:
    7
    Views:
    364
    Marco Manfredini
    Aug 13, 2004
  3. Chris
    Replies:
    1
    Views:
    658
    Chris
    May 11, 2006
  4. Edvard Majakari
    Replies:
    4
    Views:
    673
    Edvard Majakari
    Feb 25, 2005
  5. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    682
    Skybuck Flying
    Oct 4, 2009
Loading...

Share This Page