450 likes | 579 Views
xsl:for-each. The xsl:for-each element allows for repeat processing of same sibling elements. It is ideal for producing table-like output in the output document tree. <xsl:template match=“node-value”> <xsl:for-each> <!-- statements go here --> </xsl:for-each> </xsl:template>.
E N D
xsl:for-each • The xsl:for-each element allows for repeat processing of same sibling elements. • It is ideal for producing table-like output in the output document tree. <xsl:template match=“node-value”> <xsl:for-each> <!-- statements go here --> </xsl:for-each> </xsl:template>
Specifying Same Nodes • In order for the xsl:for-each elements to output content to the output document tree for each unique node, we must include the select attribute value. <xsl:template match=“node-value”> <xsl:for-each select=“node-value”> <!-- statements go here --> </xsl:for-each> </xsl:template>
xsl:variable • The xsl:variable element allows us to store a value that we can use at run time. • Variables come in two forms – global and local. • A Global variable is declared at the top of the document and can only contain one value for the entire transformation. • A Local variable is contained within a template and contains a different value each time the template is accessed.
xsl:variable • A xsl:variable element may be used as both a container and empty element with the difference being the presence or absence of the select attribute. <xsl:variable name=“temp”>abc</xsl:variable> <xsl:variable name=“temp”><xsl:value-of select=“/books/book[1]/title”/></xsl:variable> <xsl:variable name=“temp” select=“/books/book[1]/title”/>
Specifying Same Nodes • In order for the xsl:for-each elements to output content to the output document tree for each unique node, we must include the select attribute value. <xsl:template match=“node-value”> <xsl:for-each select=“node-value”> <!-- statements go here --> </xsl:for-each> </xsl:template>
XSL XPath Functions
An Example using normalize-space() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __normalize-space()__ normalize-space('CIS97yt') = '<xsl:value-of select="normalize- space(' CIS 97 yt ')"/>' normalize-space(sample-node-set) = '<xsl:value-of select="normalize-space(sample-node-set)"/>' </xsl:template> </xsl:stylesheet>
Source Document The Source XML Document…. <?xml version="1.0"?> <!-- Sample XML Document --> <sample-doc xml:lang="en"> <sample-node-set> <node-set node="1">10</node-set> <node-set node="2">20</node-set> <node-set node="3">30</node-set> </sample-node-set> </sample-doc>
An Example using string() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __string()__ string(true()) = <xsl:value-of select="string(true())"/> string(false()) = <xsl:value-of select="string(false())"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __string()__ string(true()) = true string(false()) = false
An Example using string() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __string()__ string(0) = <xsl:value-of select="string(0)"/> string(1) = <xsl:value-of select="string(1)"/> string(1.1) = <xsl:value-of select="string(1.1)"/> string(-1.1) = <xsl:value-of select="string(-1.1)"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __string()__ string(0) = 0 string(1) = 1 string(1.1) = 1.1 string(-1.1) = -1.1
An Example using string() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __string()__ string('') = <xsl:value-of select="string('')"/> string('CIS97yt') = <xsl:value-of select="string('CIS97yt')"/> string('a') = <xsl:value-of select="string('a')"/> string('97') = <xsl:value-of select="string('97')"/> string('9 7') = <xsl:value-of select="string('9 7')"/> string('9+7') = <xsl:value-of select="string('9+7')"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __string()__ string('') = string('CIS97yt') = CIS97yt string('a') = a string('97') = 97 string('9 7') = 9 7 string('9+7') = 9+7
An Example using string() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __string()__ string(node-set) = <xsl:value-of select="string(node-set)"/> string(sample-node-set) = <xsl:value-of select="string(sample-node-set)"/> string(sample-node-set/node-set) = <xsl:value-of select="string(sample-node-set/node-set)"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __string()__ string(node-set) = string(sample-node-set) = 10 20 30 string(sample-node-set/node-set) = 10
The Results…. The Output Document…. __normalize-space()__ normalize-space('CIS97yt') = 'CIS 97 yt' normalize-space(sample-node-set) = '10 20 30'
Source Document The Source XML Document…. <?xml version="1.0"?> <!-- Sample XML Document --> <sample-doc xml:lang="en"> <sample-node-set> <node-set node="1">10</node-set> <node-set node="2">20</node-set> <node-set node="3">30</node-set> </sample-node-set> </sample-doc>
An Example using boolean() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __boolean(boolean)__ boolean(false()) = <xsl:value-of select=“boolean(false())”/> boolean(true()) = <xsl:value-of select=“boolean(true())”/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __boolean(boolean)__ boolean(false()) = false boolean(true()) = true
An Example using boolean() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __boolean(number)__ boolean(0) = <xsl:value-of select=“boolean(0)”/> boolean(1) = <xsl:value-of select=“boolean(1)”/> boolean(1.1) = <xsl:value-of select=“boolean(1.1)”/> boolean(-1.1) = <xsl:value-of select=“boolean(-1.1)”/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __boolean(number)__ boolean(0) = false boolean(1) = true boolean(1.1) = true boolean(-1.1) = true
An Example using boolean() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __boolean(string)__ boolean('') = <xsl:value-of select="boolean('')"/> boolean('CS97yt') = <xsl:value-of select="boolean('CS97yt')"/> boolean('a') = <xsl:value-of select="boolean('a')"/> boolean('97') = <xsl:value-of select="boolean('97')"/> boolean('9 7') = <xsl:value-of select="boolean('9 7')"/> boolean('9+7') = <xsl:value-of select="boolean('9+7')"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __boolean(string)__ boolean('') = false boolean('CIS97yt') = true boolean('a') = true boolean('97') = true boolean('9 7') = true boolean('9+7') = true
An Example using boolean() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __boolean(node-set)__ boolean(node-set) = <xsl:value-of select="boolean(node-set)"/> boolean(sample-node-set) = <xsl:value-of select="boolean(sample-node-set)"/> boolean(sample-node-set/node-set) = <xsl:value-of select="boolean(sample-node-set/node-set)"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __boolean(node-set)__ boolean(node-set) = false boolean(sample-node-set) = true boolean(sample-node-set/node-set) = true
An Example using not() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __not()__ not(true()) = <xsl:value-of select="not(true())"/> not(false()) = <xsl:value-of select="not(false())"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __not()__ not(true()) = false not(false()) = true
An Example using not() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __not(number)__ not(0) = <xsl:value-of select=“not(0)”/> not(1) = <xsl:value-of select=“not(1)”/> not(1.1) = <xsl:value-of select=“not(1.1)”/> not(-1.1) = <xsl:value-of select=“not(-1.1)”/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __not()__ not(0) = true not(1) = false not(1.1) = false not(-1.1) = false
An Example using not() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __not(string)__ not('') = <xsl:value-of select="not('')"/> not('CS97yt') = <xsl:value-of select="not('CS97yt')"/> not('a') = <xsl:value-of select="not('a')"/> not('97') = <xsl:value-of select="not('97')"/> not('9 7') = <xsl:value-of select="not('9 7')"/> not('9+7') = <xsl:value-of select="not('9+7')"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __not()__ not('') = true not('CIS97yt') = false not('a') = false not('97') = false not('9 7') = false not('9+7') = false
An Example using not() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __not(node-set)__ not(node-set) = <xsl:value-of select="not(node-set)"/> not(sample-node-set) = <xsl:value-of select="not(sample-node-set)"/> not(sample-node-set/node-set) = <xsl:value-of select="not(sample-node-set/node-set)"/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __not()__ not(node-set) = true not(sample-node-set) = false not(sample-node-set/node-set) = false
An Example using true() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __true()__ true() = <xsl:value-of select=“true()”/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __true()__ true() = true
An Example using false() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __false()__ false() = <xsl:value-of select=“false()”/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __false()__ false() = false
An Example using lang() The Transformation Document…. <xsl:stylesheet version="1.0“ xmlns:xsl=“http://www.w3.org/1999/XSL/Transform”> <xsl:template match=“sample_doc”> __lang()__ lang(‘en’) = <xsl:value-of select=“lang(‘en’)”/> lang(‘fr’) = <xsl:value-of select=“lang(‘fr’)”/> </xsl:template> </xsl:stylesheet>
The Results…. The Output Document…. __lang()__ lang('en') = true lang('fr') = false