Menu

adding loop counter

Help
antonio
2006-11-23
2012-09-04
  • antonio

    antonio - 2006-11-23

    Hi,

    How do I add a loop counter in my output xml? any help. I am trying to add a counter value in the yahoo example...

    <file action="write" path="catalog.xml" charset="UTF-8">
        <![CDATA[ <add> ]]>
        <loop item="item" index="i">
            <list><var name="products"/></list>
            <body>
                <xquery>
                    <xq-param name="item"><var name="item"/></xq-param>
                    <xq-expression><![CDATA[
                            let $name := data($item//td[3]/h2[1])
                            let $desc := data($item//td[3]/div[@class='desc'])
                            let $price := data($item//div[@class='price']/a[1])
                                return
                                    <product>
                                        <id>{$index}</id> === Here I am stuck!!
                                        <name">{normalize-space($name)}</name>
    

    Thanks for your help

    Cheers

     
    • antonio

      antonio - 2006-11-23

      Thank you very very much! Super!

       
    • Vladimir Nikic

      Vladimir Nikic - 2006-11-23

      you cannot use templating in that way inside XQuery expression. You need to introduce new parameter using xq-param. Something like this:

      <xquery>
      <xq-param name="item"><var name="item"/></xq-param>
      <xq-param name="index"><template>&lt;index&gt;${i}&lt;/index&gt;</template></xq-param>
      <xq-expression><![CDATA
      let $name := data($item//td[@colspan='1' and @rowspan='1' and @class='desc'
      //h2//a[1])
      let $desc := data($item//td[@colspan='1' and @rowspan='1' and @class='desc']/div[1])
      let $price := data($item//div[@class='price']/a[1])
      let $counter := data($index//index)
      return
      <product>
      <id>{$counter}</id>
      <name>{normalize-space($name)}</name>
      <desc>{normalize-space($desc)}</desc>
      <price>{normalize-space($price)}</price>
      </product>
      ]]></xq-expression>
      </xquery>

       

Log in to post a comment.