Because XML can be used to represent any vocabulary (often defined by some schema), the question is how these different vocabularies can be processed and maybe transformed into something else. This something else
may be another XML vocabulary (a common requirement in B2B scenarios), or it may be HTML (a common scenario for Web publishing). Using XSL Transformations (XSLT), mapping tasks can be implemented easily. XSLT leverages XPath's expressive power in a rather simple programming language, the programs are often called stylesheets. For easy tasks, XSLT mappings can be specified without much real programming
going on, by simply specifying how components of the source markup are mapped to components of the target markup.
<xsl:for-each select=".//a">
<xsl:if test="not(@title)">
<xsl:text>This link has no title!</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:for-each select=".//a[not(@title)]">
<xsl:text>This link has no title!</xsl:text>
</xsl:for-each>
default behaviormay seem strange
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> </xsl:stylesheet>
textof the document is produced
appliedby the XSLT processor
spaghetti code
Hello Worldin XSLT
hello worldtherefore ignores the input
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:text>Hello World!</xsl:text> </xsl:template> </xsl:stylesheet>
<xsl:for-each select="/people/entry">
<tr>
<td><xsl:value-of select="name/pre[1]"/></td>
<td><xsl:value-of select="name/sur"/></td>
<td><a href="mailto:{email}"><xsl:value-of select="email"/></a></td>
</tr>
</xsl:for-each>
spaghetti code
applied)
<xsl:template match="/ | *"> <xsl:text>( Element: </xsl:text> <xsl:value-of select="local-name()"/> <xsl:apply-templates select="@* | *"/> <xsl:text>)</xsl:text> </xsl:template> <xsl:template match="@*"> <xsl:text>Attribute: </xsl:text> <xsl:value-of select="local-name()"/> </xsl:template>
* matches any elementtr matches tr elementsthead/tr matches tr elements within thead elementsp[@class='warning'] matches p elements with their class set to warning|)<xsl:template match="* | /"> <xsl:apply-templates/> </xsl:template>
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> </xsl:stylesheet>
<xsl:template match="text() | @*"> <xsl:value-of select="."/> </xsl:template>
<xsl:template match="processing-instruction() | comment()"/>
besttemplate must be found
00.25-0.250.5<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates select="descendant::a"/> </xsl:template> <xsl:template match="*"/> <xsl:template match="a"/> <xsl:template match="b/a"/> <xsl:template match="c/b/a"/> </xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?> <c> <b> <a/> </b> </c>
| Pattern | Priority | Resolution Step | Manual Adjustment |
||||
|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | |||
Built-in: |
✓ | ✓ | |||||
Built-in: |
✓ | ✓ | ✓ | ||||
|
-0.5 | ✓ | ✓ | ✓ | ✓ | ||
|
0.0 | ✓ | ✓ | ✓ | ✓ | ||
|
0.25 | ✓ | ✓ | ✓ | ✓ | ✓ | |
|
0.25 | ✓ | ✓ | ✓ | ✓ | ✓ | priority="1" |
-0.5 and 0.50.5overwritingtemplates, the behavior can be customized
<xsl:variable name="sum" select="$op1 + $op2"/> <xsl:variable name="result" select="$sum * $factor"
<xsl:value-of select="$email-prefix"/> <!-- $email-prefix = 'You have ' --> <xsl:value-of select="count(//message)"/> <xsl:value-of select="$email-suffix"/> <!-- $email-suffix = ' e-mail messages.' -->
updated, they have to be re-created
variablesif they are constants?
<xsl:variable name="name" select="'value'"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$name = 'value'">
<xsl:variable name="result" select="'ok'"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="result" select="'problem'"/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$result"/>
</xsl:template>
<xsl:variable name="name" select="'value'"/>
<xsl:template match="/">
<xsl:variable name="result">
<xsl:choose>
<xsl:when test="$name = 'value'">
<xsl:value-of select="'ok'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'problem'"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$result"/>
</xsl:template>
<xsl:param name="number" select="0"/> <xsl:variable name="number-param"> <xsl:if test="string(number($number)) = 'NaN'"> <xsl:message terminate="yes">"number" must be a proper number!</xsl:message> </xsl:if> <xsl:value-of select="$number"/> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="10 * $number-param"/> </xsl:template>
main param start = 1 ; param count = 10 ; {
loop (0) };
loop param counter ; {
print $start + $counter ;
if ( $counter < $count - 1) then
loop ($counter + 1) ; }
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:text>me: </xsl:text>
<xsl:value-of select="count(document('document.xsl')/descendant::*)"/>
<xsl:text>
http://dret.net/netdret/publications: </xsl:text>
<xsl:value-of select="count(document('http://dret.net/netdret/publications')/descendant::*)"/>
</xsl:template>
</xsl:stylesheet>
<p>do <u>not</u> <em>throw</em> <b>away</b> these whitespace nodes!</p>
...Key)
<people> <entry id="dret" country="de"> <name> <pre>Erik</pre> <pre>Thomas</pre> <sur>Wilde</sur> </name> <email>dret@berkeley.edu</email> <affiliation country="us">iSchool/UCB</affiliation> <phone location="office" type="voice">+1-510-6432253</phone> <phone location="office" type="fax">+1-510-6425814</phone>
<xsl:key name="preNameKey" match="name" use="pre"/> <xsl:key name="affiliationKey" match="affiliation" use="."/> <xsl:key name="countryKey" match="entry | affiliation" use="@country"/>
|
|
||||||||||||||||||||||||||||
$a[count(. | $b) = count($b)]: Find all nodes in $a where the cardinality of $b does not change when adding this node to it. This means the node must be in $b, and it is in $a to start with.
<xsl:template match="*">
<xsl:element name="{translate(local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}">
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{translate(local-name(), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>