Loading ...
Sorry, an error occurred while loading the content.

Re: [XSL-FO] adding indexed variables

Expand Messages
  • John E. Simpson
    ... This kind of incrementation is a classic use-case for either the XPath position() function or the xsl:number XSLT element. The former is simple and
    Message 1 of 2 , Sep 26, 2001
    • 0 Attachment
      At 01:21 PM 09/26/2001 +0000, rtobin@... wrote:
      > I am using XSL to add data and input boxes into a html page!
      > how can I alter the name and id to include an incrementing varibale?
      >...
      > so that: <input id=gen1 name=gen1 value......>
      > <input id=gen2 name=gen2 value......>
      > <input id=gen3 name=gen3 value......> is produced

      This kind of incrementation is a classic use-case for either the XPath
      position() function or the xsl:number XSLT element. The former is simple
      and straightforward when you need to increment within a single source-tree
      node-set; the latter gives you lots of options for number formatting and
      doing things like numbering (say) paragraphs within sections within chapters.

      In your case, one thing which may be confusing you is how to get these
      values into the result tree. If you want to use position(), just try
      something like this (within your xsl:for-each loop):

      <input>
      <xsl:attribute name="id">
      <xsl:value-of select="concat('gen', position())"/>
      </xsl:attribute>
      <xsl:attribute name="name">
      <xsl:value-of select="concat('gen', position())"/>
      </xsl:attribute>
      <xsl:attribute name="value">
      <xsl:value-of select="."/>
      </xsl:attribute>
      </input>

      (Personally, I prefer to use the concat() function for readability, rather
      than mixing #PCDATA content with an xsl:value-of -- visually, I always kind
      of lose the thread when I see something like:
      <xsl:attribute name="id">gen<xsl:value-of
      select="position()"/></xsl:attribute>
      But the #PCDATA-with-xsl:value-of works just fine.)

      The xsl:number element without any attributes at all works like position().
      For instance:

      <xsl:attribute name="id">gen<xsl:number/></xsl:attribute>

      Hope that helps.

      =======================================================================
      John E. Simpson | "I saw a sign: 'Rest Area 25 Miles'. That's
      http://www.flixml.org | pretty big. Some people must be really tired."
      XML Q&A: www.xml.com | (Steven Wright)
    Your message has been successfully submitted and would be delivered to recipients shortly.