cancel
Showing results for 
Search instead for 
Did you mean: 

Need Help in XSLT Mapping

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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

stefan_grube
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Stefan,

I am new to XSLT mapping.

I want to generate a sequence number in every node using XSLT mapping. Can you please tell me what would be the code for getting sequence number in my resultant XML

Thanks

Chinnu

former_member181962
Active Contributor
Former Member
0 Kudos

Ravi,

Thanks for ur reply. The specified link is, really realted to my problem, but the discussion, in the link is greek n latin to me . Any how, I will try to explore something more. If you have any code which is simple then plz help me.

Thanks

Chinnu