The XML Tags

printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    

JSTL: JSP Standard Tag Library Kick Start
By Jeff Heaton

Table of Contents
Appendix A.  JSTL Reference


XML is becoming a common form of data encountered on the Internet. JSTL makes available several XML tags that allow XML documents to be formatted and processed. Most of the XML tags use XPath expressions to specify the operation you want to perform. XPath is covered in Chapter 8.

The <x:choose> Tag

The <x:choose> tag is used to specify a mutual exclusion block. Inside the <x:choose> tag's body, <x:when> and <x:otherwise> tags reside. The first <x:choose> tag that is satisfied will execute the tags inside the <x:choose> tag's body. If no <x:choose> tags are satisfied, then the <x:otherwise> tag, if present, will be executed. The <x:choose> tag has no attributes.

<x:choose> body content (<x:when> and <x:otherwise> subtags) </x:choose> 

The <x:forEach> Tag

The <x:forEach> tag allows you to iterate through child nodes of an XML document. The <x:forEach> tag works much like the <c:forEach> tag, in that the body is executed for each item in the collection. As the loop progresses, each item is copied to the var attribute.

<x:forEach [var="varName"] select="XpathExpression"> body content </x:forEach> 

The <x:forEach> tag has the following attributes:

Attribute

Required

Purpose

select

Y

An XPath expression to iterate over.

var

N

Specifies the scoped variable that will hold each iteration.

The <x:if> select Tag

The <x:if> tag is used to perform basic logic on the XML document that has been parsed. Using the <x:if> statement, you can examine individual elements in the node you specify using the XPath data in the select attribute.

// Syntax 1: Without body, result copied to var    <x:if select="XpathExpression"    var="varName" [scope="{page|request|session|application}"]/> // Syntax 2: With conditional body content    <x:if select="XpathExpression"    [var="varName"] [scope="{page|request|session|application}"]>       body content    </x:if> 

The <x:if> tag has the following attributes:

Attribute

Required

Purpose

scope

N

Specifies the scope for the scoped variable referenced by the var attribute. The default is page.

select

Y

An XPath expression that should be evaluated (true or false).

var

N

Specifies the scoped variable that can be set to the result of the

if
statement.

The <x:otherwise> Tag

The <x:otherwise> tag is used as part of an <x:choose> block. If none of the <x:when> tags is satisfied, then the <x:choose> block will execute the <x:otherwise> tag. The <x:otherwise> tag does not have any attributes.

<x:otherwise> conditional block </x:otherwise> 

The <x:out> Tag

The <x:out> tag is used to display individual elements from the XML document.

<x:out select="XpathExpression" [escapeXml="{true|false}"]/> 

The <x:out> tag has the following attributes:

Attribute

Required

Purpose

escapeXml

N

Specifies whether the displayed data should have its special characters escaped. Defaults to true.

select

Y

An XPath expression that you want to display.

The <x:param> Tag

The <x:param> tag is used to specify parameters that can be passed to the XSLT script being executed by the <x:transform> tag. The <x:param> tag should be used only inside the body of an <x:transform> tag.

// Syntax 1: Parameter value specified in attribute value   <x:param name="name" value="value"/> // Syntax 2: Parameter value specified in the body content   <x:param name="name">   parameter value   </x:param> 

The <x:param> tag has the following attributes:

Attribute

Required

Purpose

name

Y

Specifies the name of this parameter.

value

N

Specifies the value of this parameter.

The <x:parse> Tag

The <x:parse> tag is used to parse an XML document. This tag must be called before any of the other XML tags can be used. Once the document has been parsed, it may be fed to the other XML tags for processing.

// Syntax 1: XML document specified via a String or Reader object   <x:parse {xml="XMLDocument"}   [filter="filter"]   [system]   [{var="varName" [{scope="{page|request|session|application}"] |varDom="varName"}[scopeDom="{page|request|session|application}"}]]/> // Syntax 2: XML document specified via the body content   <x:parse [filter="filter"]   [system]   [{var="varName" [{scope="{page|request|session|application}"]   scopeDom="{page|request|session|application}"}]>   XML Document to parse   </x:parse> 

The <x:parse> tag has the following attributes:

Attribute

Required

Purpose

filter

N

Allows a filter, of the type org.xml.sax.XMLFilter, to be used to filter the incoming XML data.

scope

N

Specifies the scope of the scoped variable referenced by the var attribute. Defaults to page.

scopeDom

N

Specifies the scope of the scoped variable referenced by the varDom attribute. Defaults to page.

systemId

N

Allows a system ID to be specified to return information about the parse.

var

N

Specifies the scoped variable to hold the parsed XML document.

varDom

N

Specifies the scoped variable that will receive the XML document as an org.w3c.dom.Document object.

xml

N

Specifies the XML that is to be parsed.

The <x:set> Tag

The <x:set> tag is used to set a scoped variable to one of the values found inside the XML document.

<x:set select="XpathExpression" var="varName" [scope="{page|request|session|application}"]/> 

The <x:set> tag has the following attributes:

Attribute

Required

Purpose

scope

N

Specifies the scope for the scoped variable referenced by the var attribute. The default is page.

select

Y

An XPath expression that you want to display.

var

Y

Specifies the scoped variable that is to be set.

The <x:transform> Tag

The <x:transform> tag is used to transform an XML document using the specified XSLT template. This allows you to use server-side XSLT in conjunction with your JSTL Web applications.

// Syntax 1: No body content   <x:transform   xml="XMLDocument" xslt="XSLTStylesheet"   [xmlSystem] [xsltSystem]   [{var="varName" [scope="scopeName"]|result="resultObject"}] // Syntax 2: Transformation parameters specified using body   <x:transform   xml="XMLDocument" xslt="XSLTStylesheet"   [xmlSystem] [xsltSystem]   [{var="varName" [scope="scopeName"]|result="resultObject"}]   <x:param> actions   </x:transform> // Syntax 3: Using a body to both specify an XML document and   transformation parameters   <x:transform   xslt="XSLTStylesheet"   xmlSystem xsltSystem   [{var="varName" [scope="scopeName"]|result="resultObject"}]   XML document being processed   optional <x:param> actions   </x:parse> 

The <x:transform> tag has the following attributes:

Attribute

Required

Purpose

result

N

A scoped variable of type javax.xml.transform.Result that specifies how the result should be processed.

scope

N

Specifies the scope of the scoped variable referenced by the var attribute. Defaults to page.

var

N

Specifies a scoped variable that will hold the transformed document. If none is specified, the results will be written to the page.

xml

N

Specifies a string that holds the XML file to be used with the transformation.

xslt

Y

Specifies a string that holds the XSLT file to be used with the transformation.

The <x:when> Tag

The <x:when> tag should be used inside the body of an <x:choose> tag. The first <x:when> tag that is satisfied will execute any tags in its body. If no <x:when> tags are satisfied, then the <x:otherwise> tag will be executed.

<x:when select="XPathExpression"> body content </x:when> 

The <x:when> tag has one attribute:

Attribute

Required

Purpose

select

Y

An XPath expression that you want to evaluate.


    printer-friendly version of this section  Print  e-mail this section  E-Mail  add a public, group or private note  Add Note  add a bookmark about this section  Add Bookmark    
    Top

    [0672324504/app01lev1sec4]

     
     


    JSTL. JSP Standard Tag Library Kick Start
    JSTL: JSP Standard Tag Library Kick Start
    ISBN: 0672324504
    EAN: 2147483647
    Year: 2001
    Pages: 93
    Authors: Jeff Heaton

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