XSLT

Aus Claudio's Wiki
Wechseln zu: Navigation, Suche

XSLT ist Bestandteil von XSL. XSLT steht für EXtensible Stylesheet Language Transformations und dient der Transformation von z.B. XML-Dateien in anderen XML-Dateien oder HTML-Dateien.

Deklaration

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

oder

<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

XSLT Elements

<xsl:template>

Templates on w3cschools.com

<xsl:template match="/"> => heisst gesamtes Dokument

Syntax

<xsl:template name="name" match="pattern" mode="mode" priority="number">

 <!-- Content:(<xsl:param>*,template) -->

</xsl:template>

Attributes
Attribute Value Optional Description
name name yes Name für das Template.
Wichtig: Wenn nicht angegeben, dann muss ein match-Attribut definiert werden!
match pattern yes Pattern (XPath), dass zutreffen muss.
Wichtig: Wenn nicht angegeben, dann muss ein name-Attribut definiert werden!
mode mode yes Mit dem mode-Attribut kann man verschiedene Templates mit gleichem Namen definieren und trotzdem auseinander halten.
priority number yes Eine Nummer, die die Priorität angibt.


<xsl:apply-templates>

Apply-Templates on w3cschools.com


Syntax

<xsl:apply-templates select="expression" mode="name">

 <!-- Content:(xsl:sort|xsl:with-param)* -->

</xsl:apply-templates>

Attributes
Attribute Value Optional Description
select expression yes Ist eine XPath Expression (node-set-expression).
* heisst alle nodes.
Wenn nicht angegeben, werden alle Kinder-Nodes ausgewählt.
mode name yes Ist eine Möglichkeit, templates mit selben Namen zu unterscheiden.



<xsl:output>

xsl:output on w3cschools.com


Syntax

<xsl:output
method="xml|html|text|name"
version="string"
encoding="string"
omit-xml-declaration="yes|no"
standalone="yes|no"
doctype-public="string"
doctype-system="string"
cdata-section-elements="namelist"
indent="yes|no"
media-type="string"/>

Attributes
Attribute Value Optional Description
method xml|html|
text|name
yes Defines the output format. The default is XML (but if the first child of the root node is <html> and there are no preceding text nodes, then the default is HTML)

Netscape 6 only supports "html" and "xml"

version string yes Sets the W3C version number for the output format (only used with method="html" or method="xml")
encoding string yes Sets the value of the encoding attribute in the output
omit‑xml‑declaration yes|no yes "yes" specifies that the XML declaration (<?xml...?>) should be omitted in the output. "no" specifies that the XML declaration should be included in the output. The default is "no"
standalone yes|no yes "yes" specifies that a standalone declaration should occur in the output. "no" specifies that a standalone declaration should not occur in the output. The default is "no"

This attribute is not supported by Netscape 6

doctype‑public string yes Sets the value of the PUBLIC attribute of the DOCTYPE declaration in the output
doctype‑system string yes Sets the value of the SYSTEM attribute of the DOCTYPE declaration in the output
cdata‑section‑elements namelist yes A white-space separated list of elements whose text contents should be written as CDATA sections
indent yes|no yes "yes" indicates that the output should be indented according to its hierarchic structure. "no" indicates that the output should not be indented according to its hierarchic structure.

This attribute is not supported by Netscape 6

media‑type string yes Defines the MIME type of the output. The default is "text/xml"

This attribute is not supported by Netscape 6



<xsl:preserve-space> / <xsl:strip-space>

Artikel noch nicht fertig

The <xsl:preserve-space> element is used to define the elements for which white space should be preserved.
The <xsl:strip-space> element is used to define the elements for which white space should be removed.
Note
Preserving white space is the default setting, so using the <xsl:preserve-space> element is only necessary if the <xsl:strip-space> element is used.
Note
The <xsl:preserve-space> element and the <xsl:strip-space> element are top-level elements.

syntax:

<xsl:preserve-space elements="list-of-element-names"/>

<xsl:strip-space elements="list-of-element-names"/>


example:

<xsl:preserve-space elements="div a span" />

<xsl:strip-space elements elements="th td" />

Attributes
Attribute Value Optional Description
elements list-of-element-names no A white space separated list of element names for which white space should be preserved/removed.

Note: The list can also contain "*" and "prefix:*" so that all elements or all elements from a particular namespace can be joined.


<xsl:key>

Artikel noch nicht fertig

The <xsl:key> element is a top-level element which declares a named key that can be used in the style sheet with the key() function.
Note: A key does not have to be unique!

syntax:

<xsl:key name="name" match="pattern" use="expression"/>

example

Suppose you have an XML file called "persons.xml":

<persons>
  <person name="Tarzan" id="050676"/>
  <person name="Donald" id="070754"/>
  <person name="Dolly" id="231256"/>
</persons>

You can define a key in an XSL file like this:

<xsl:key name="preg" match="person" use="@id"/>


To find the person with id="050676", write (in the XSL file):

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="preg" match="person" use="@id"/>

<xsl:template match="/">
  <html>
  <body>
  <xsl:for-each select="key('preg','050676')">
    <p>
    Id: <xsl:value-of select="@id"/><br />
    Name: <xsl:value-of select="@name"/>
    </p>
  </xsl:for-each>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>


Attributes
Attribute Value Optional Description
name name no Specifies the name of the key
match pattern no Defines the nodes to which the key will be applied
use expression no The value of the key for each of the nodes



<xsl:include> / <xsl:import>

<xsl:include>

<xsl:import>

syntax:

<xsl:import href="URI"/>

<xsl:include href="URI"/>

<xsl:include>
allows you to include one stylesheet into another.
The included stylesheet's templates will have the same default priorities and import precedence as the including stylesheet.

<xsl:import>
offers the same, but the import precedence of elements in an imported stylesheet
is always less than that of the importing stylesheet.


example:


Attributes
Attribute Value Optional Description
href URI yes Specifies the URI of the style sheet to import/include.



<xsl:value-of>

Kurzform einer Ausgabe
Erfolgt die Ausgabe innerhalb von Attributen kann man anstelle eines value-of's eine geschweifte Klammer verwenden.

Beispiel Kurzform:

<xsl:template match="img">
 <img src="{src}" alt="{@alt}"/>
</xsl:template>

Ist etwas eleganter als die längere Lösung (Ein kleiner Tipp: Sie könnten gar nicht <img name="<xsl:value-of select="."/>"/> verwenden weil Tags grundsätzlich beendet (nicht geschlossen) sein müssen wenn man eine XSL:value-of-Ausgabe anfängt. Sie müssten mit <xsl:element> arbeiten).


<xsl:variable>

  • Variablen sind read-only. Einmal beschrieben lassen sie sich nicht mehr ändern.
  • Die variable ist global wenn sie ein top-level element ist, und lokal wenn sie in einem template deklariert ist.
  • Wenn man den attribut select verwendet, kann man nichts mehr in die variable schreiben ()

Syntax:

<xsl:variable name="name" select="expression">


</xsl:variable>

<xsl:when>

The <xsl:when> element is used to specify an action for the <xsl:choose> element. The <xsl:when> element evaluates an expression, and if it returns true, an action is performed.

Note: The <xsl:when> element is used in conjunction with <xsl:choose> and <xsl:otherwise> to express multiple conditional tests.

Syntax:

<xsl:when test="boolean-expression">


</xsl:when> >

xsl-when

Output

Normalerweise schützt (escaped) die xml-Ausgabemethode & und < (und möglicherweise andere Zeichen) bei der Ausgabe von Textknoten. Falls dies nicht erwünscht ist, kann man es mit disable-output-escaping="yes" deaktivieren.

Beispiel:

<xsl:text disable-output-escaping="yes">&lt;</xsl:text>

erzeugt <.


disable output escaping Deutsch

Externe Links

Tutorials


References