Java and XSLT reading notes (1)

Source: Internet
Author: User
Tags date expression html tags string sort string format version variable
Notes "Java and XSLT" reading notes

First, all XSLT processors must include four built-in template rules that are less prioritized than any other rule, so you can overwrite them by writing a new template rule to match the same style. The best way to understand the built-in rules is to set them up always in the background and apply these built-in rules if no other rule is found that matches one node.

<xsl:template match= "*|/" >

<xsl:apply-templates/>

</xsl:template>

<xsl:template match= "*|/" mode= "M" >

<xsl:apply-templates mode= "M"/>

</xsl:template>

<xsl:template match= "text () |@*" >

<xsl:value-of select= "." />

</xsl:template>

<xsl:template match= "Processing-instruction () |comment ()" >

Two, XPath

<message>


<subject>Hello,World</subject>

<date mm= "dd=" yy= "2002" >

<sender>pres@whitehouse.gov</sender>

<recipient>burke_e@ociWeb.com</recipient>

<recipient>burke_e@yahoo.com</recipient>

<recipient>aidan@burke.com</recipient>


<body>

...

</body>

</message>

If
Simply adding a predicate to filter the result node set usually reduces the size of the result set. Additional predicates can be added for additional filtering. For example, Child::recipient[position () =1] initially selects all <recipient> elements from the previous instance and then filters down (reduces) the list until the first: Burke_ The e@ociWeb.com position starts at 1 instead of 0. Predicates can contain any XPath expression and become quite complex.

Such as:

<xsl:apply-templates select= "presidents/president[position () =3]/name"/>

<xsl:apply-templates select= "Presidents/president[count (vicepersident) =0]/name"/>

<xsl:apply-templates select= "presidents/president[term/@from < 1800]/name"/>

<xsl:apply-templates select= "descendant::p resident[count (vicepresident) >1]/name"/>

<xsl:apply-templates select= "presidents/president/name[child::first= ' John '"/>

<xsl:apply-templates

Select= "presidents/president[(term/@from > 1800) and (term/@from < 1850)]/name" >

Three, sort

Sorting can be applied to data drivers or to template-driven methods. In either case, <xsl:sort> is added as a child element to the other part. Multiple-field sorting can be done by adding several contiguous <xsl:sort> elements. Each sort can be in ascending and descending order, and the data type used for sorting can be either "number" or "text." sorting defaults to Ascending.

<xsl:sort select= "a"/>

<xsl:sort select= "Last" order= "descending"/>

<xsl:sort select= "term/@from" order= "descending" data-type= "number"/>

<xsl:sort select= "Name/first" data-type= "text" case-order= "Upper-first"/>



Four, attribute value template (AVT)

As long as we need to treat an attribute value as an XPath expression rather than static text, we may need to use a AVT. However, for standard XSLT elements, such as <xsl:template match= "pattern"/&gt, you do not have to use the AVT syntax. For non-XSLT elements, such as arbitrary HTML tags, AVT syntax is required.

Five,<xsl:attribute-set>

<xsl:element name= "Body" use-attribute-sets= "Body-style" ></xsl:element>

Six,<xsl:if>

<xsl:if test= "Boolean-expression" >

<!--@@@-->

</xsl:if>

<xsl:if test= "Middle" > Check for a node set instead of a Boolean value

<xsl:if test= "@someAttribute"

Seven

<xsl:choose>

<xsl:when test= "party= ' democratic '" >

<xsl text>blue</xsl:text>

</xsl:when>

<xsl:otherwise>

<xsl:text>red</xsl:text>

</xsl:otherwise>

<xsl:choose>



Eight, parameters and variables

<xsl:variable name= "lastpresident" select= "president[position" () = Last ()]/name "/>"

<xsl:value-of select= "$lastPresident"/>

Nine, naming templates

<xsl:template name= "FORMATSSN" >

<xsl:param name= "ssn"/>

</xsl:template>

<xsl:call-template name= "FORMATSSN" >

<xsl:with-param name= "ssn" select= "@ssn"/>

</xsl:call-template>



Ten, Increment variable

<!---Familytree.xml->

<?xml version= "1.0" encoding= "UTF-8"?>

<?xml-stylesheet type= "text/xsl" href= "Familytree.xslt"?>

<person name= "Otto" >

<person name= "Sandra" >

<person name= "Jeremy" >

<person name= "Eliana"/>

</person>

<person name= "Eric" >

<person name= "Aidan"/>

</person>

<person name= "Philip" >

<person name= "Alex"/>

<person name= "Andy"/>

</person>

</person>

</person>



<!---familytree.xslt->

<?xml version= "1.0" encoding= "UTF-8"?>

<xsl:stylesheet version= "1.0" xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >

<xsl:output method= "html"/>



<!--processing begins here-->

<xsl:template match= "/" >


<body>

<!--Select the top level person-->

<xsl:apply-templates select= "Person" >

<xsl:with-param name= "level" select= "' 0"/>

</xsl:apply-templates>

</body>


</xsl:template>



<!--Output information for a and recursively select

All children. -->

<xsl:template match= "Person" >

<xsl:param name= "Level"/>



<!--indent according to the level-->

<div style= "text-indent:{$level}em" >

<xsl:value-of select= "@name"/>

</div>



<!--recursively select children, incrementing the level-->

<xsl:apply-templates select= "Person" >

<xsl:with-param name= "Level" select= "$level + 1"/>

</xsl:apply-templates>

</xsl:template>

</xsl:stylesheet>

11, Template mode

<xsl:template match= "name" mode= "verbose" >

</xsl:template>



12,<xsl:include> and <xsl:import>

<xsl:include> can only be a top-level element, that is, it is a brotherhood with <xsl:template>

<xsl:include href= "Url-reference"/>

Problem occurs when two conflicting template rules are included

<xsl:import> must appear before any other top-level element in <xsl:stylesheet>



13, formatting text and values

String Format-number (number, String, string?)

United States dollar $#.# #0 #和0是数字占位符并严格符合java. Text.decimalfromat specified behavior

0% output a percentage



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.