jstl xml variables for select attribute

2019-08-10 09:25发布

问题:

I'm using jstl xml to get specified values from an XML

Here I'm using a "userRoleSelector" as a variable to select the node. The variable is set as,

<c:set var="userRoleSelector" value="role-id='test'" scope='session'/>

The tag where I use the variable is as follows,

<x:if select="$parsedRoleXML//roles/role[$userRoleSelector]/features/feature[text()='viewEvents']">
        //html code
</x:if>

The node is not selected according to the role-id provided. it works if I use only the right hand side as a variable.

What is the issue here?

回答1:

There are a few problems here:

  • The XPath predicate syntax [$userRoleSelector] is not doing a comparison
  • The XPath predicate syntax is searching for an index, not an attribute value
  • userRoleSelector is not an XPath variable
  • c:set variables are not in the XML scope, unlike x:parse variables
  • userRoleSelector is a JSP variable

To test the role-id properly, try this instead:

[@role-id='test']

To test the JSP variable, try this instead:

[@role-id=$sessionScope:userRoleSelector]

References

  • XPath: Variable Reference
  • JSP - XML Data
  • JSR-52: jstl-1_0-fr-spec.pdf


标签: xml jsp jstl