problem with variable

Here should go questions about transforming XML with XSLT and FOP.
heldopslippers
Posts: 10
Joined: Thu Dec 07, 2006 12:08 pm

problem with variable

Post by heldopslippers »

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: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Post by george »

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
Joined: Thu Dec 07, 2006 12:08 pm

Post by heldopslippers »

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: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

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
Joined: Thu Dec 07, 2006 12:08 pm

Post by heldopslippers »

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: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

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
Post Reply