Hi,
I've an XML document and I am creating another XML using XSL. I need to check some specific conditions and for that I want to use Javascript in my XSL. I tried it, however, couldn't get the desired result. As I could not change the XSL variables frequiently so i am trying to use Javascript.

XSL-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:es="http://ucmservice"
    version="1.0" xmlns="http://filenet.com/namespaces/wcm/apps/1.0" xmlns:java="http://xml.apache.org/xalan/java" xmlns:js="urn:custom-javascript" xmlns:lxslt="http://xml.apache.org/xslt"
        xmlns:totalSys="TotalSystem"  extension-element-prefixes="totalSys">
    
  <lxslt:component prefix="totalSys" functions="checkFirstProp">
  <lxslt:script lang="javascript">

            var firstPropVal = "";
            var secondPropVal = "";
            var completedFirstPropVal= new Array();
            var completedSecondPropVal= new Array();
            var firstDecisionFlag = 0; 
            function checkFirstProp(xmlValue)
            {
                firstDecisionFlag = 0;
                
                if(firstPropVal.length == 0)
                {
                        firstPropVal = xmlValue;
                        firstDecisionFlag = 1; 
                 }
                 else
                 {
                    if(firstPropVal != xmlValue)
                    {
                         firstPropVal = xmlValue; 
                         firstDecisionFlag = 2; 
                    }
                  }
                  
                  return firstDecisionFlag; 
            }
     
       </lxslt:script>
 </lxslt:component>
 
<xsl:template match="/">
     <xsl:apply-templates select="XMLTag"/>
</xsl:template>

<xsl:template match="XMLTag">        
        <xsl:variable name="firstPropDecisionFlag">
            <xsl:value-of select="totalSys:checkFirstProp(param)"/> 
        </xsl:variable>
        
        <xsl:if test="$firstPropDecisionFlag=2">
            {
                task
            } 
        </xsl:if>
</xsl:template>
</xsl:stylesheet>

Please advise, where am i going wrong?

Everything looks ok to me from an XSLT perspective. I'm not familiar with javascript as such, so I can't comment on if that's the issue.

What are the requirements for the test or flag that you're trying to set? It should be pretty easily done in XSLT. I can definitely help there if there are clear requirements.

Thanks iceandrews.

There were some jar files needed. I've added them and now its working.
However, I am still stuck with implementing some logic in XSL. Please check and advise.

In my XSL I want to close a node only if a particular condition is true. I don't know how to implement it-

XSL i am trying-

<xsl:template match="element">
		<xsl:variable name="nameAttribute0">
			<xsl:value-of
				select="es:RepositoryDetails/es:Repository/@Type" />
		</xsl:variable>
		<xsl:variable name="firstPropDecisionFlag">
			<xsl:value-of
				select="totalSys:checkFirstProp(string($nameAttribute0))" />
		</xsl:variable>

		<xsl:if test="$firstPropDecisionFlag=2">
			<![CDATA[
              </item>
              ]]>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="$firstPropDecisionFlag &gt; 0">
				<![CDATA[
                <item level="0" name="{$nameAttribute0}">
                ]]>
                
                ***<!-- At this place i am facing issues.. despite of picking it as a node and substituting the value of {$nameAttribute0} it take it as a simple string. if i take it out of CDATA it will ask for the end tag that i want to put only at a particular condition.. Please Advice -->***
                 
				<xsl:apply-templates
					select="es:DocProperties/es:UniqueDocId/es:DocProperty[1]">
					<xsl:sort select="@propertyName" />
				</xsl:apply-templates>

			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates
					select="es:DocProperties/es:UniqueDocId/es:DocProperty[1]">
					<xsl:sort select="@propertyName" />
				</xsl:apply-templates>
			</xsl:otherwise>

		</xsl:choose>

	</xsl:template>

Without seeing the input document and sample output it's hard to understand what you're trying to do. Can you construct a simple input / output scenario? Feel free to strip it own to the basics so we can get to heart of the problem.

It seems like you're building fragments of tags. That's not the right approach per say. You shouldn't be building half a tag, should be building the whole nodeset that you want.

I am not building half a tag. its only that the tag needs to be ended only once a condition is true.

the output XML that i want is like-

<?xml version="1.0" encoding="UTF-8"?><tree xmlns="http://filenet.com/namespaces/wcm/apps/1.0" xmlns:lxslt="http://xml.apache.org/xslt" xmlns:es="http://ucmservice">
<tree>
  <item level="0" name="Admin">
	<item name="Index" level="1">
		<item name="Gurgaon" level="2"/>
	</item>
	<item name="Index Document" level="1">
		<item name="Gurgaon" level="2"/>
	</item>
	<item name="Document" level="1">
		<item name="Gurgaon" level="2"/>
	</item>
  </item>
  <item level="0" name="Hello">
	<item name="Treat index Document ABC" level="1">
		<item name="Gurgaon" level="2"/>
	</item>
	<item name="Index Document" level="1">
		<item name="Gurgaon" level="2"/>
	</item>
  </item>
</tree>

Sample input XML-

<feed xmlns:es="http://ucmservice">
    <element>
        <es:RepositoryDetails>
            <es:Repository Type="DatabaseRepository"/>
            <es:ConnetionDetails>
                <es:Param MigrationSetId="439"/>
            </es:ConnetionDetails>
        </es:RepositoryDetails>
        <es:DocProperties>
            <es:UniqueDocId Value="_1_15">
                <es:DocProperty propertyName="EmployeeName">
                    <es:PropValues>
                        <es:PropValue>Index</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="EmployeeAddress">
                    <es:PropValues>
                        <es:PropValue>Gurgaon</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Salary">
                    <es:PropValues>
                        <es:PropValue>15000</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Employee_id">
                    <es:PropValues>
                        <es:PropValue>40179575</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Sex">
                    <es:PropValues>
                        <es:PropValue>F</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Experience">
                    <es:PropValues>
                        <es:PropValue>7</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="department">
                    <es:PropValues>
                        <es:PropValue>ETS</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
            </es:UniqueDocId>
           </es:DocProperties>
    </element>
    <element>
        <es:RepositoryDetails>
            <es:Repository Type="DatabaseRepository"/>
            <es:ConnetionDetails>
                <es:Param MigrationSetId="439"/>
            </es:ConnetionDetails>
        </es:RepositoryDetails>
        <es:DocProperties>
            <es:UniqueDocId Value="_1_12">
                <es:DocProperty propertyName="EmployeeName">
                    <es:PropValues>
                        <es:PropValue>Index Document</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="EmployeeAddress">
                    <es:PropValues>
                        <es:PropValue>Gurgaon</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Salary">
                    <es:PropValues>
                        <es:PropValue>20000</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Employee_id">
                    <es:PropValues>
                        <es:PropValue>40179572</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Sex">
                    <es:PropValues>
                        <es:PropValue>M</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="Experience">
                    <es:PropValues>
                        <es:PropValue>2</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
                <es:DocProperty propertyName="department">
                    <es:PropValues>
                        <es:PropValue>HR</es:PropValue>
                    </es:PropValues>
                </es:DocProperty>
            </es:UniqueDocId>            
        </es:DocProperties>
    </element>
</feed>

now how many nodes are to come at level 1 and level 2 is decided at the run time. for that i've applied the logic in javascript and its working fine, however this part is creating issues for me.

You are building half a tag. Your logic is saying "START the item TAG", and apply these tempaltes, then when that's done you should "END the item TAG". That's not how XSLT works.

You have to build the entire item node at once.

<item>
DO SOMETHING
</item>

If you need to STOP doing something after a certain point, the logic of that recursion and when to stop should be built into the the "doing something portion", not to when to start or end tag. There's no such thing as a "tag" in xslt. BTw I sent you a private message with some more questions above your samples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.