Jump to content

how to create an array in xslt


shwetharaya

Recommended Posts

Hi All,How to create an array in xslt.<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://mycompany.com/mynamespace"> <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/> <msxsl:script language="javascript" implements-prefix="user"> <![CDATA[ function Rolecheck(roles,like,fullstrings) { var splitstring = fullstrings.split(";"); var testbool = 0; if (like == 'no') { var i=0; for (;splitstring:) { if (roles == splitstring) testbool = 1; if (testbool == 1) break; i++; } if (testbool == 1) return 'yes'; else return 'no'; } else { var i=0; for (;splitstring:) { var idx=splitstring.indexOf(roles) if(idx==0) testbool = 1; if (testbool == 1) break; i++; } if (testbool == 1) return 'yes'; else return 'no'; } } ]]> </msxsl:script> <xsl:template match="/"> <html> <body> <h2>Roles</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Role Name</th> <th align="left">Role Desc</th> </tr> </table> <xsl:variable name="fullstring"> <xsl:for-each select="/xslt-info/user-info/user-core/profile/entitlement-roles/entitlement-role"> <xsl:value-of select="entitlement-role-code"> </xsl:value-of>;</xsl:for-each> </xsl:variable> <xsl:value-of select="$fullstring" /> <p> <xsl:variable name = "test"> <xsl:value-of select="user:Rolecheck('test','no',string($fullstring))"/> </xsl:variable> <br/> <xsl:value-of select = "$test"></xsl:value-of> </p> </body> </html> </xsl:template></xsl:stylesheet>This is my code. i want to send an array from my xslt to java script. can u tell me how to create an array in xslt and how it can be passed to java script

Link to comment
Share on other sites

Things don't work out this way, so you can't do that. Why would you need to do that anyway?If you tell me what are you trying to achieve, I might be able to help you do it without that sort of need.

Link to comment
Share on other sites

Things don't work out this way, so you can't do that. Why would you need to do that anyway?If you tell me what are you trying to achieve, I might be able to help you do it without that sort of need.
Hi Robot,This is my code.<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:user="http://mycompany.com/mynamespace" xmlns:wsml="urn:org:wsml:2003:html" xmlns:str="http://exslt.org/strings" xmlns:msxsl="urn:schemas-microsoft-com:xslt"> <xsl:output method='html' version='1.0' encoding='UTF-8' indent='yes'/> <msxsl:script language="javascript" implements-prefix="user"> <![CDATA[ function Rolecheck(roles,like,fullstrings) { var splitstring = fullstrings.split(";"); var testing = roles.split(";"); var matching = like.split(";"); var testbool = 0; var k=0; for(;matching[k]:) { if (matching[k] == 'no') { var i=0; var j=0; for (;splitstring:) { for (;testing[j]:) { if (testing[j] == splitstring) testbool = 1; if (testbool == 1) break; j++; } i++; } if (testbool == 1) return 'yes'; else return 'no'; } else { for (i=0;i<splitstring.length-1;) { for(j=0;j<testing.length-1;) { var idx=splitstring.indexOf(testing[j]) if(idx==0) testbool = 1; if (testbool == 1) break; j++; } i++; } if (testbool == 1) return 'yes'; else return 'no'; } } k++; } ]]> </msxsl:script> <xsl:template match="/"> <html> <body> <h2>Roles</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Role Name</th> <th align="left">Role Desc</th> </tr> </table> <xsl:variable name="fullstring"> <xsl:for-each select="/User/Roles/Role"> <xsl:value-of select="Rolename"> </xsl:value-of>;</xsl:for-each> </xsl:variable> <xsl:value-of select="$fullstring" /><br/> <xsl:variable name="setrole"> <values>ggg</values> <values>abc</values> <values>eag</values> <values>def</values> </xsl:variable> <xsl:variable name="display_role"> <xsl:for-each select="msxsl:node-set($setrole)/values"> <xsl:value-of select="."/> <xsl:text>;</xsl:text> </xsl:for-each> </xsl:variable> <xsl:value-of select="$display_role"/><br/> <xsl:variable name="match"> <values>yes</values> <values>no</values> <values>no</values> <values>yes</values> </xsl:variable> <xsl:variable name="match_role"> <xsl:for-each select="msxsl:node-set($match)/values"> <xsl:value-of select="."/> <xsl:text>;</xsl:text> </xsl:for-each> </xsl:variable> <xsl:value-of select="$match_role"/> <p> <xsl:variable name = "test"> <xsl:value-of select="user:Rolecheck(string($display_role),string($match_role),string($fullstring))"/> </xsl:variable> <br/> <xsl:value-of select = "$test"></xsl:value-of> </p> </body> </html> </xsl:template></xsl:stylesheet>.let me explain u the requirment.I have to create a common template. where i have set of user role which is taken from XML and set of roles which will be given by the extenal user to check aganist this set of user roles taken from XML.<xsl:value-of select="user:Rolecheck(string($display_role),string($match_role),string($fullstring))"/> </xsl:variable>this is where i call my javascript passing string($display_role),parameter which is an (array)string where the external user has passed the set of rolesstring($match_role),this paramtere is an (array)string which has one to one mapping with the above array, which say's whether it has to check the roles aganist user roles for exact match or like match.this inputs are again given by external user .string($fullstring), this paramter is nothing but user roles taken from XMLso basically wat i am trying to do here is , i have to check the a set of roles aganist a set of user roles whichis always taken from XML , whether the role passed by the external user (set of roles) is present in the ser of user roles taken from XML , if present it has to return me YES or NO staisfying the 2 condition here :they are:1. along with checking it has to check for whether the user wants to check for exact match or like match. i.e for example : ANY ROLE WHICH STARTS FROM ABC WILL BE LIKE MATCH . A ROLE SHOULD BE MATCT EXACTLY AS IT IS EX:ABCDEF So i am using an array here to have a one to one mapping with array of set of roles given the external user .even this match array is given by the external user whether the role he is checking is for exact match or like match.2. I need check one more condition is for AND or OR condtion. Say i have a condtion (xsl:if test =( "( role1 & role2) || (role4 & role5)") something like this . ii need to replace this whole by a common template which will check whether these rolesi.e role 1 role2 role3 role4 these are given by extenal user array . we need to check whether these roles are present in the set of roles taken from XML and also check whether the external user is checking for LIkE or EXact match. and also check for the AND or OR condition along with parenthesis satisifying. after checking for the all these condition it shoukd return me YES or NO. please let me know if it is possible.
Link to comment
Share on other sites

On the first reading, it seems as you need some sort of a complex XSLT key but as this article suggests, you might also be able to achive this with a few conditions. You can add a few conditions in the same test like this for example:<xsl:if test="role1 = role2 and role4 = role5">And you can even add more weird conditions by using the boolean() function:<xsl:if test="role1 = role2 and boolean(role4 = role5 or role6 = role7)">I still don't completely grasp the test you need to make, but I'm sure it's doable with XSLT. Either conditions or keys. Good luck with the keys though. I'm not into them much yet.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...