2
votes

I want to use XML attribute's value and store and compare it in the XSL file. I am converting the XSL to an HTML through a JAVA program, which is working fine. The output HTML is:-

Value =

XML file:

<root>
<Request>
    <Desc>Insert new team into the table</Desc>
    <Param name="teamName" required="false"/>
    <Param name="rankings" required="true"/>
    <Update requires="teamName,rankings" >
        INSERT INTO standing (teamName,rankings) VALUES($rankings,$rankings)
    </Update>
</Request>

XSL file:

<xsl:template match="root">
    <xsl:variable name="UpdateRequires"/>
    <html>
        <head>
            <title>DocStyle.xsl</title>
        </head>
        <body> 
            <xsl:for-each select="*/Request">
                <xsl:for-each select="*/Update">
                    <xsl:variable name="UpdateRequires" select="*/Update/@requires"/>
                </xsl:for-each>
            </xsl:for-each>
            <h1>
               Value = <xsl:value-of select="$UpdateRequires"/>
            </h1>
        </body>
    </html>
</xsl:template>

I want to display the value of the variable "UpdateRequires", and then compare it with Param tag's attribute "@name" maybe like this "contains(@name,UpdateRequires)"

UPDATE 1.0:

I am able to fetch the value of the variable, now I want to compare the value of the variable $UpdateRequires and the value of the attribute @name. It should return true for contains(@name,$UpdateRequires) which it's not doing (the if loop with test="@name" was just to check the values)

Changes made to XSL:

<xsl:for-each select="Request">
                <xsl:variable name="UpdateRequires" select="*/@requires"/>
                <xsl:for-each select="Update">
                    <xsl:variable name="UpdateRequires" select="@requires"/>
                    <h1>
                        We are in Update : <xsl:value-of select="$UpdateRequires"/>
                    </h1>    
                </xsl:for-each>
                <xsl:for-each select="Param">
                    <xsl:if test=" contains(@name,$UpdateRequires) ">
                        <span>
                            We are in Param : <xsl:value-of select="$UpdateRequires"/>
                        </span>
                    </xsl:if>
                    <xsl:if test=" @name ">
                        <span>
                            We are in Param : <br/> Value of variable : <xsl:value-of select="$UpdateRequires"/> Value of name : <xsl:value-of select="@name"/> <br/>
                        </span>
                    </xsl:if>
                </xsl:for-each>
2
Will your XML have more than one Request? And can a Request have more than one Update?michael.hor257k
There can be more <Request>, and there can be more than one <Update> tags as well.Sukhinderpal Mann
"there can be more than one <Update> tags" Then it's not clear what you're trying to do. The contains() function takes strings as arguments, you cannot feed it a node-set.michael.hor257k
I was just trying to compare values of two attributes in two different tags. Anyway thanks for the help I found the answer. But contain() function was returning false for some reason.Sukhinderpal Mann

2 Answers

2
votes

There are several issues with your approach:

First, your XPath expressions are wrong: Request is a child of root, not its grandchild - so your instruction <xsl:for-each select="*/Request"> does nothing.

Likewise, <xsl:for-each select="*/Update"> will do nothing from the context of Request.

Next, a variable's scope is limited to its parent element: if you define a variable within xsl:for-each, you cannot use it outside this instruction.


Try perhaps something like:

<xsl:template match="/root">
    <html>
        <head>
            <title>DocStyle.xsl</title>
        </head>
        <body> 
            <xsl:for-each select="Request">
                <xsl:variable name="UpdateRequires" select="Update/@requires"/>
                <h1>
                   Value = <xsl:value-of select="$UpdateRequires"/>
                </h1>
             </xsl:for-each>
        </body>
    </html>
</xsl:template>

Note that this assumes there can be several Requests, but each will have only one Update child.

1
votes
<xsl:for-each select="Request">
                <xsl:variable name="UpdateRequires" select="*/@requires"/>
                <xsl:for-each select="Update">
                    <xsl:variable name="UpdateRequires" select="@requires"/>
                    <h1>
                        We are in Update : <xsl:value-of select="$UpdateRequires"/>
                    </h1>    
                </xsl:for-each>
                <xsl:for-each select="Param">
                    <xsl:if test=" matches($UpdateRequires,@name) ">
                        <span>
                            We are in Param : <xsl:value-of select="$UpdateRequires"/><br/>
                        </span>
                    </xsl:if>
                </xsl:for-each>
            </xsl:for-each>

contains() function wasn't returning true (can someone tell me why?) hence I used matches(). And it works now.