on 06-03-2008 4:06 PM
Hi,
I am trying to get global container variable in my XSLT mapping using UDF. Where can I create the string variable "a" in XI?
My UDF is as follows.
int counter = 1;
GlobalContainer gc = container.getGlobalContainer();
if (gc.getParameter(a) != null) {
counter = Integer.parseInt(String.valueOf(gc.getParameter(a)));
counter = counter + 1;
}
gc.setParameter(a, String.valueOf(counter));
return String.valueOf(counter);
Note:- I am not using Msg Mapping editor.
Thanks
Raja
Hi Raja,
you have to use variables(xsl:variable) to achieve the goal. try this with the same source of above:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:element name="customers">
<xsl:for-each select="/customers/customer">
<xsl:variable name="counter" select="'1'"/>
<xsl:element name="{name()}">
<xsl:for-each select="*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:element name="a1"><xsl:value-of select="$counter"/></xsl:element>
<xsl:element name="a2"><xsl:value-of select="$counter+1"/></xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
if this dosen't fill your requirements, you can adjust it having a try with variables...
Regards,
Emiliano
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I don't know if I've understood exactly what you need, but try this:
If you have a source code as the following:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
</customer>
<customer>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
</customer>
</customers>
And you try this xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<xsl:element name="customers">
<xsl:for-each select="/customers/customer">
<xsl:element name="{name()}">
<xsl:element name="Counter">
<xsl:value-of select="position()"/>
</xsl:element>
<xsl:for-each select="*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
You will get the result:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<Counter>1</Counter>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
</customer>
<customer>
<Counter>2</Counter>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
</customer>
</customers>
Regards,
Emiliano
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Emiliano,
Yes,your solution is very helpfull. I got the answer from you, what I am expecting, but I need to maintain a global counter to refer in the sub nodes.
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<Counter>1</Counter>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
</customer>
<customer>
<Counter>2</Counter>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
</customer>
</customers>
In the above code, I can get two more nodes in the customer tag under companyName like a1 and a2.
In a1, I have to put sequential number and in a2 I have to refer to the parent counter.
Thanks
Chinnu
Hi,
I do have 8 templates in my XSLT code. I am using this tag <xsl:value-of select="position()"/> in each and every template. It's working fine with first template resultant XML. Whenever the control goes to second template, again its starting from 1 rather than continuation from the last number in the first template. your inputs are highly appreciated.
Thanks
Chinnu
I think "a" should be a string constant.
you can use:
gc.setParameter("anyName", String.valueOf(counter));
Integer.parseInt((String) gc.getParameter("anyName"));
On the other side: You can attach any self-written Java class to XSLT, that might be easier to code a counter without use of global container.
Regards
Stefan
Edited by: Stefan Grube on Jun 3, 2008 5:24 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Check this link:
http://www.velocityreviews.com/forums/t168696-xslt-maintaining-iteration-count.html
Regards,
ravi
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.