11.9 XML Core Actions

   

11.9 XML Core Actions

The XML core actions comprise three actions that let you parse XML, evaluate XPath expressions and display the result, and set scoped variables specified with XPath expressions. Table 11.23 lists the XML core actions.

Table 11.23. XML Core Actions

Action

Description

<x:parse>

Parses an XML document.

<x:out>

Outputs the result of an XPath expression.

<x:set>

Sets a scoped variable's value to the result of an XPath expression.

<x:parse>

Parses an XML document and stores the result in a scoped variable

Syntax: [29]

[29] Items in brackets are optional.

Syntax #1: Without a body; specifying an XML document as a String or Reader

 <x:parse xml [systemId] [filter] {var [scope]  varDom [scopeDom]}/> 

Syntax #2: With a body; specifying an XML document in the action's body

 <x:parse [systemId] [filter] {var [scope]  varDom [scopeDom]}  xml  </x:parse> 

Description:

The <x:parse> action parses an XML document that you specify with the xml attribute or in the body of the <x:parse> action. The result of the parsed document is stored in a scoped variable, which you can specify with either var and scope attributes or the varDom and scopeDom attributes.

The <x:parse> action does not perform validation against DTDs or Schemas.

Attributes:

Attribute [a]

Type

Description

xml

String or java.io.Reader

A string or a reader that represents an XML document that's parsed by <x:parse>. Note: This attribute does not represent a filename.

systemId

String

The system identifier used to resolve external entities.

filter

org.xml.sax.XMLFilter

A SAX filter that filters the document as it's parsed.

var

String

The name of a scoped variable that references an object that represents the parsed XML document.

scope

String

The scope of the scoped variable whose name is specified by the var attribute; default is page scope.

varDom

String

The name of a scoped variable that references a DOM tree structure representing the parsed XML document.

scopeDom

String

The scope of the varDom scoped variable; default is page scope.

[a] static dynamic

Constraints and Error Handling:

  • If you specify a null value or an empty string for the xml attribute, <x:parse> will throw a JspException .

  • If you specify a null value for the filter attribute, <x:parse> will not perform any filtering.

In a Nutshell:

There are a few things to keep in mind when you use the <x:parse> action:

  • The xml attribute does not represent a filename . It must be a string or a reader whose content represents an XML document. Typically, this means that you will use <c:import> to convert an XML document to a string or a reader, which is subsequently specified as the <x:parse> action's xml attribute.

  • Do you want <x:parse> to use DOM ? The <x:parse> action stores the result of a parsed XML document in a scoped variable. You can specify that variable with the var attribute, and you can optionally specify that variable's scope with the scope attribute. If you use the var attribute (and optionally, the scope attribute), it's up to the JSTL implementation to decide what type of object to use for that scoped variable, so it may be a DOM tree structure or it may be another data type ”presumably more efficient ”that is specific to your JSTL implementation. If you specify the varDom attribute (and optionally, the scopeDom attribute), you will force the JSTL implementation to use a DOM tree structure.

  • Do you want to filter for performance? For large XML documents, it can be inefficient to store the parsed document in a data structure, whether it's DOM or not. If you only access a subset of the data stored in an XML document, you can use a SAX filter, which you specify with the <x:parse> filter attribute; <x:parse> uses that filter to filter the parsed document. The entire document is still parsed, but only the filtered subset is stored in a data structure.

<x:out>

Evaluates an XPath expression, coerces that expression to a string, and sends the result to the current JspWriter

Syntax: [30]

[30] Items in brackets are optional.

 <x:out select [escapeXml]/> 

Description:

The <x:out> action is analogous to the <c:out> action, which evaluates an EL expression and sends the result to the current JspWriter . The <x:out> action does the same, except that it evaluates an XPath expression.

Attributes:

Attribute [a]

Type

Description

select

String

An XPath expression.

escapeXml

boolean

Determines whether some characters are converted to character entity codes; see "The <c:out> Action" on page 102 for more information about those characters and their entity codes.

[a] static dynamic

In a Nutshell:

The <x:out> action is similar to <c:out>. Both actions evaluate an expression, coerce that expression to a string, and send the result of that evaluation to the current JspWriter , but <c:out> evaluates an EL expression whereas <x:out> evaluates an XPath expression. By default, both actions escape certain characters, but you can defeat that functionality by specifying false for the escapeXml attribute. See "<c:out>" on page 469 for more information about XML escaping. The <x:out> action does not let you specify a default value, as is the case for the <c:out> action.

<x:set>

Evaluates an XPath expression and stores the result in a scoped variable

Syntax: [31]

[31] Items in brackets are optional.

 <x:set select var [scope]/> 

Description:

The <x:set> action is analogous to the <c:set> action, which evaluates an EL expression and stores the result in a scoped variable. The <x:out> action does the same, except that it evaluates an XPath expression.

Attributes:

Attribute [a]

Type

Description

Select

String

An XPath expression.

Var

String

The name of the scoped variable in which the result of the XPath expression is stored.

Scope

String

The scope of the scoped variable whose name is specified by the var attribute; default is page scope.

[a] static dynamic

   


Core JSTL[c] Mastering the JSP Standard Tag Library
Core JSTL[c] Mastering the JSP Standard Tag Library
ISBN: 131001531
EAN: N/A
Year: 2005
Pages: 124

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net