Search for

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • barkeeper@jubii.de

    Search for

    Hi,

    I have a xml input file like this:

    <root>
    <element>
    <name>elem1</name>
    <child>
    <name>1</name>
    </child>
    <child>
    <name>2</name>
    </child>
    </element>
    <element>
    <name>elem2</name>
    <child>
    <name>2</name>
    </child>
    <child>
    <name>3</name>
    </child>
    </element>

    Now I want apply a template on every element element that has a child
    with name="3" or name="1" for example.

    I do this right now:
    <xsl:for-each-group select="root/elment" group-by="name">
    <xsl:if test="contains( current()/child/name, '3') or
    contains(curren t()/child/name, '1')" >
    <xsl:apply-templates select="current-group()"/>
    </xsl:if>
    </xsl:for-each-group>

    But I get a "too many items" error. I guess because there are more
    than one child nodes in a elment node.

    Thanks
    Peter
  • Pavel Lepin

    #2
    Re: Search for


    barkeeper@jubii .de <barkeeper@jubi i.dewrote in
    <fe0c4300-8c99-40a0-810e-38a65d580fab@q2 1g2000hsa.googl egroups.com>:
    <root>
    <element>
    <name>elem1</name>
    <child>
    <name>1</name>
    </child>
    <child>
    <name>2</name>
    </child>
    </element>
    <element>
    <name>elem2</name>
    <child>
    <name>2</name>
    </child>
    <child>
    <name>3</name>
    </child>
    </element>
    Not well-formed.
    Now I want apply a template on every element element that
    has a child with name="3" or name="1" for example.
    <xsl:apply-templates
    select="/root/element[child/name/text()='1' or
    child/name/text()='3']"/>
    <xsl:for-each-group select="root/elment" group-by="name">
    <xsl:if test="contains( current()/child/name, '3') or
    contains(curren t()/child/name, '1')" >
    <xsl:apply-templates select="current-group()"/>
    </xsl:if>
    </xsl:for-each-group>
    I think I'll just add xsl:for-each-group to my list of Evil
    Stuff That Confuses Neophytes.

    --
    When all you have is a transformation engine, everything
    looks like a tree.

    Comment

    • Joseph Kesselman

      #3
      Re: Search for

      barkeeper@jubii .de wrote:
      Now I want apply a template on every element element that has a child
      with name="3" or name="1"
      <xsl:apply-templates select="/root/element[child/name=3 or child/name=1]"/>

      --
      Joe Kesselman / Beware the fury of a patient man. -- John Dryden

      Comment

      • barkeeper@jubii.de

        #4
        Re: Search for

        Well this works, but not like I need it.
        I should have mentioned what I do in my template that I apply.

        I want to output the element names but only once the same.
        With the current solution I get the element names many times, to be
        exactly once for every child that's name is 1 or 3.

        That is why i grouped the names in a for-each-group loop.



        On 11 Feb., 16:22, Joseph Kesselman <keshlam-nos...@comcast. net>
        wrote:
        barkee...@jubii .de wrote:
        Now I want apply a template on every element element that has a child
        with name="3" or name="1"
        >
        <xsl:apply-templates select="/root/element[child/name=3 or child/name=1]"/>
        >
        --
        Joe Kesselman / Beware the fury of a patient man. -- John Dryden

        Comment

        • Joseph Kesselman

          #5
          Re: Search for

          barkeeper@jubii .de wrote:
          I want to output the element names but only once the same.
          See the XSLT FAQ website's "grouping" page; that should give you some
          useful techniques.

          --
          Joe Kesselman / Beware the fury of a patient man. -- John Dryden

          Comment

          • Martin Honnen

            #6
            Re: Search for

            barkeeper@jubii .de wrote:
            Well this works, but not like I need it.
            I should have mentioned what I do in my template that I apply.
            >
            I want to output the element names but only once the same.
            With the current solution I get the element names many times, to be
            exactly once for every child that's name is 1 or 3.
            >
            That is why i grouped the names in a for-each-group loop.
            I think it is better you provide a sample of your XML that makes it
            clear what you need to group on and then describe the result you want to
            create with your stylesheet.
            So far we can only guess what you want, here is my attempt at guessing

            <xsl:for-each-group select="root/element[child/name = '1' or
            child/name = '3']" group-by="name">
            <xsl:value-of select="current-grouping-key()"/>
            <xsl:if test="position( ) != last()">
            <xsl:text</xsl:text>
            </xsl:if>
            </xsl:for-each-group>

            It outputs the grouping key of each group.



            --

            Martin Honnen

            Comment

            • barkeeper@jubii.de

              #7
              Re: Search for

              Hi folks,

              that was the clue:
              <xsl:for-each-group select="root/element[child/name = '1' ....

              Now it works.
              Thanks very much!

              I really have my problems with this xslt stuff :-)

              Bye Peter



              On 11 Feb., 17:48, Martin Honnen <mahotr...@yaho o.dewrote:
              barkee...@jubii .de wrote:
              Well this works, but not like I need it.
              I should have mentioned what I do in my template that I apply.
              >
              I want to output the element names but only once the same.
              With the current solution I get the element names many times, to be
              exactly once for every child that's name is 1 or 3.
              >
              That is why i grouped the names in a for-each-group loop.
              >
              I think it is better you provide a sample of your XML that makes it
              clear what you need to group on and then describe the result you want to
              create with your stylesheet.
              So far we can only guess what you want, here is my attempt at guessing
              >
              <xsl:for-each-group select="root/element[child/name = '1' or
              child/name = '3']" group-by="name">
              <xsl:value-of select="current-grouping-key()"/>
              <xsl:if test="position( ) != last()">
              <xsl:text</xsl:text>
              </xsl:if>
              </xsl:for-each-group>
              >
              It outputs the grouping key of each group.
              >
              --
              >
              Martin Honnen
              http://JavaScript.FAQTs.com/

              Comment

              Working...