Learn how to use the type-aware and pattern-aware XSLT 2.0 in debugging and http://www.aliyun.com/zixun/aggregation/8775.html > Test processes to avoid invalid paths to data types and cardinality, Common problems with error assumptions. In addition, this article provides examples of XSLT style representations that contain errors that cannot be caught without using the pattern-aware attribute. You will learn how to explicitly specify type results in useful error messages.
XML content can be complex and unpredictable. If you use XSLT 1.0 to process XML content, you will experience a lot of trial and error procedures when defining the correct expression and handling all possible content structures. For example, the misspelled name in an XPath 1.0 expression causes no content to be returned, rather than providing a useful error message, and therefore difficult to debug.
XSLT 2.0 has made great strides in comparison with version 1.0, where new strong types and schema perceptions are very useful features. Declaring a value type in an XSLT stylesheet enables the processor to tell you when a false assumption about the occurrence of an XML content data type, a particular element, or attribute has been made. This feature will provide more useful error messages. The import XML schema further reinforces this feature, providing sufficient information to the processor to understand the input document structure so that you are notified when an invalid XPath occurs, rather than returning nothing. They also provide information about the type of data in the content and avoid performing meaningless operations on the corresponding data types.
Using Types in XSLT
Most programming languages provide methods for specifying a variable or parameter type. In XSLT 2.0, you can use the As property to declare an expression type that can appear in multiple locations:
, in xsl:variable or Xsl:param elements, indicates the type of the variable or parameter in the xsl:template or xsl:function element, indicating that the return type of the template or function is in the xsl:sequence element, indicating that the sequence type is in the Xsl:with-param element, indicating the value type passed to a template
The value of the As property is also called a sequence type.
Built-in types using XML schemas
A sequence type commonly used in the as attribute is the name of a particular data type, such as a string or integer. In XSLT, you can use one of the data types built into the XML schema specification. For this, for example, you can use xs: prefix the type name and declare the xs: namespace at the top of the style sheet. Table 1 lists the most commonly used XML schema data types.
Table 1. Common XML Schema data types
data type name Description sample string any text string abc is an integer 1, 2 decimal decimal 1.2 of any size of a string integer, 5.0 double double-precision floating-point number 1.2, 5.0 date yyyy format 2009-12-25 Time HH:MM:SS Format 12:05:04 boolean true/False Anyatomictype value string of any simple type, 123, False, 2009-12-25
For example, if you want to declare a variable value as a date, I can use:
<variable name= "effdate" select= "Bill/effdate" as= "Xs:date"/>
Simple values such as strings and dates are also called atomic values. Note that if Effdate is an element, its contents are converted to an atomic value of the xs:date type (assuming there is no pattern associated with this input document).
Represents the sequence type of an XML node
You can also use the more general sequence types in table 2 to represent the node types in the XML tree. You do not need to use xs: as prefixes for these sequence types because they are not part of the XML schema type.
Table 2. Represents the sequence type of an XML node
sequence type description element () any element attribute () any element element (book) named Book () any attribute () arbitrary attribute (ISBN) name is an ISBN any property text () any text node () Any type of node (element, attribute, text node, etc.) item () Any kind of node, or any kind of atomic value (such as String, Integer)
For example, if I want to represent a value that is bound to a variable as an element, you can say:
<variable name= "effdate" select= "//effdate" as= "element ()"/>
Unlike the previous example, this value is not converted to an atomic value;