problem with variable

Here should go questions about transforming XML with XSLT and FOP.
heldopslippers
Posts: 10

problem with variable

Mon Jan 29, 2007 1:42 pm

I have a question:

I have this flexibel database that can containt multiple FIELDS:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
    <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
      <METADATA>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="getal" TYPE="TEXT"/>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="abc" TYPE="TEXT"/>
      </METADATA>
      </RESULTSET>
    </FMPXMLRESULT>




I want that my XSL file counts how many FIELDS there are. I tryed serveral things but it doesn't work. this is one of my XSL files I tryed:


Code: Select all


<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
  <xsl:output method = "xml"/>
  <xsl:template match = "/">
    <xsl:element name="object">
     <xsl:variable name="times2">0</xsl:variable>
      <xsl:for-each select="/FS:FMPXMLRESULT/FS:METADATA/FS:FIELD" xmlns:FS="http://www.filemaker.com/fmpxmlresult">
         <xsl:variable name="times">$times2+1</xsl:variable>
         <xsl:variable name="times2">$times</xsl:variable>
     </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>





I hope someone can help me out. :wink:

gr "heldop"
george
Site Admin
Posts: 2100

Mon Jan 29, 2007 11:04 pm

This should be as simlple as

<xsl:value-of select="count(/FS:FMPXMLRESULT/FS:METADATA/FS:FIELD)" xmlns:FS="http://www.filemaker.com/fmpxmlresult"/>

Regards,
George
George Cristian Bina
heldopslippers
Posts: 10

Thu Feb 01, 2007 4:25 pm

thanxs for the replay george,

it works fine. But I still need a increment function. (I think that my exsample wasn't good enouth)

so here is one where I need increment:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
    <FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
      <METADATA>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="getal" TYPE="TEXT"/>
        <FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="abc" TYPE="TEXT"/>
      </METADATA>
   </RESULTSET>
 </FMPXMLRESULT>



I have here an XSL code.

the XSL file need to give eats a id number.

the output have to be this:
12

Code: Select all

<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
  <xsl:output method = "xml"/>
  <xsl:template match = "/">
    <xsl:element name="object">
     <xsl:variable name="times">
      <xsl:variable name="id'>0</variable>
       <xsl:for-each select="/FS:FMPXMLRESULT/FS:METADATA/FS:FIELD" xmlns:FS="http://www.filemaker.com/fmpxmlresult">
        <xsl:variable name="id"><xsl:value-of select="$id+1"/></variable>
        <xsl:value-of select="$id"/>
     </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>


I hope someone can help me. I searched a lot for this. I couldn't vind a good selution.

gr "heldop"
sorin_ristache
Posts: 4144

Thu Feb 01, 2007 5:11 pm

Hello,

I do not understand what is the expected output of the XSLT stylesheet that you need to write. What is 12? How should it be obtained from the input XML document? What is the expected output XML document?


Regards,
Sorin
heldopslippers
Posts: 10

Thu Feb 01, 2007 5:29 pm

oh sorry

end I see that I made a mistake in my XSL file (the variable "times" doesn't do enything)

this is the XSL file:

Code: Select all

<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0" >
  <xsl:output method = "xml"/>
  <xsl:template match = "/">
    <xsl:element name="object">
      <xsl:variable name="id'>0</variable>
       <xsl:for-each select="/FS:FMPXMLRESULT/FS:METADATA/FS:FIELD" xmlns:FS="http://www.filemaker.com/fmpxmlresult">
        <xsl:variable name="id"><xsl:value-of select="$id+1"/></variable>
        <xsl:value-of select="$id"/>
     </xsl:for-each>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>




I wil discribe wat is supose to be doeing:

-it makes a variable ID
-it wil search for a field (<xsl:for-each select="/FS:FMPXMLRESULT/FS:METADATA/...)
-it puts in variable ID the ID+1 (the first time will be 0+1 =1 the second time 1+1 = 2)
-it wil write down the value of id (so in this case first 1 and then 2 (so 12))
-it wil search for the second time for a field \
-it puts in variable ID the ID+1
-it wil write down the value of id (so that wil be 1+1 = 2)
-it wil search for a field but won't find one so stops the for-each "loop"
-the XSL file is finist


I hope this wil be a bit clearer :wink:


gr "heldop"[/code]
sorin_ristache
Posts: 4144

Tue Feb 06, 2007 4:26 pm

Hello,

It seems you want to count the FS:FIELD elements. You can do that with the following stylesheet that uses the count(/FS:FMPXMLRESULT/FS:METADATA/FS:FIELD) expression:

Code: Select all

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:template match="/">
        <xsl:element name="object">
            <xsl:value-of select="count(/FS:FMPXMLRESULT/FS:METADATA/FS:FIELD)"
                xmlns:FS="http://www.filemaker.com/fmpxmlresult"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>


The output for the above XML input is:

Code: Select all

<object>2</object>


If you want to read more about XSLT stylesheets you can find XSLT tutorials here and here.


Regards,
Sorin

Return to “XSLT and FOP”

Who is online

Users browsing this forum: No registered users and 0 guests