<html>
<head>
<title>substring()-Funktion</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Beschreibung">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">substring()-Funktion</td>
<td valign="top" class="COMPATIBILITY">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
</tr>
<tr>
<td valign="top" colspan="2" class="description">
Diese Funktion gibt einen Teil eines bestimmten Strings zur&uuml;ck. Das zweite und das dritte Argument bestimmen, welcher Teil des Strings zur&uuml;ckgegeben wird. Das zweite Argument legt die Position des ersten Zeichens des Teil-Strings fest, und das optionale dritte Argument legt fest, wie viele Zeichen zur&uuml;ckgegeben werden sollen. </td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Eingaben</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Die Funktion <span class="LITERAL">substring()</span> akzeptiert einen String und eine oder zwei Zahlen als Argumente. Der String ist der String, aus dem der Teil-String extrahiert wird. Das zweite Argument wird als Anfangsposition f&uuml;r den zur&uuml;ckgegebenen Teil-String verwendet, und das optionale dritte Argument legt fest, wie viele Zeichen zur&uuml;ckgegeben werden.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Ausgabe</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Mit zwei Argumenten (einem String und einer Anfangsposition) gibt die Funktion <span class="LITERAL">substring()</span> alle Zeichen im String ab der Anfangsposition zur&uuml;ck. Beachten Sie, dass das erste Zeichen in einem XPath-String die Position 1 hat, nicht etwa 0.</p>
<p>Mit drei Argumenten (ein String, eine Anfangsposition und eine L&auml;ngenangabe) gibt die Funktion <span class="LITERAL">substring()</span> alle Zeichen im String zur&uuml;ck, deren Position gr&ouml;&szlig;er oder gleich der Anfangsposition ist und deren Position kleiner oder gleich der Anfangsposition plus der L&auml;nge ist.</p>
<p>Normalerweise sind die Argumente f&uuml;r die Funktion <span class="LITERAL">substring()</span> Ganzzahlen, wenngleich sie auch kompliziertere Ausdr&uuml;cke sein k&ouml;nnen. Einige ungew&ouml;hnliche F&auml;lle finden Sie im Abschnitt &quot;Beispiel&quot; weiter unten.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Definition</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XPath-Abschnitt 4.2, Zeichenkettenfunktionen</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Beispiel</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Um die Funktionsweise von <span class="LITERAL">substring()</span> zu veranschaulichen wird das folgende XML-Dokument verwendet:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;test&gt;
  &lt;p&gt;This is a test XML document used by several 
    of our sample stylesheets.&lt;/p&gt;
  &lt;question&gt;
    &lt;text&gt;When completed, the Eiffel Tower was the 
    tallest building in the world.&lt;/text&gt;
    &lt;true&gt;You're correct!  The Eiffel Tower was the 
    world's tallest building until 1930.&lt;/true&gt;
<!--<?troff .Nd 10?>-->
    &lt;false&gt;No, the Eiffel Tower was the world's 
    tallest building for over 30 years.&lt;/false&gt;
  &lt;/question&gt;
  &lt;question&gt;
    &lt;text&gt;New York's Empire State Building knocked the 
    Eiffel Tower from its pedestal.&lt;/text&gt;
    &lt;true&gt;No, that's not correct.&lt;/true&gt;
    &lt;false&gt;Correct!  New York's Chrysler Building, 
    completed in 1930, became the world's tallest.&lt;/false&gt;
  &lt;/question&gt;
&lt;/test&gt;</pre></span>
<p>Hier das verwendete Stylesheet:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  &lt;xsl:output method="text"/&gt;

  &lt;xsl:variable name="newline"&gt;
&lt;xsl:text&gt;
&lt;/xsl:text&gt;
  &lt;/xsl:variable&gt;

  &lt;xsl:template match="/"&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Tests of the substring() function:&lt;/xsl:text&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring('Now is the time', 4)="&lt;/xsl:text&gt;
    &lt;xsl:value-of select="substring('Now is the time', 4)"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring('Now is the time', 4, 6)="&lt;/xsl:text&gt;
    &lt;xsl:value-of select="substring('Now is the time', 4, 6)"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring('Now is the time', 4, -6)="&lt;/xsl:text&gt;
    &lt;xsl:value-of select="substring('Now is the time', 4, -6)"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring('Now is the time', -3, 6)="&lt;/xsl:text&gt;
    &lt;xsl:value-of select="substring('Now is the time', -3, 6)"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring('Now is the time', 54, 6)="&lt;/xsl:text&gt;
    &lt;xsl:value-of select="substring('Now is the time', 54, 6)"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
<!--<?troff .Nd 10?>-->
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   count(//*)=&lt;/xsl:text&gt;
    &lt;xsl:value-of select="count(//*)"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring('Here is a really long string', &lt;/xsl:text&gt; 
    &lt;:xsl:text&gt;count(//*))="&lt;/xsl:text&gt;
    &lt;xsl:value-of 
      select="substring('Here is a really long string', count(//*))"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring('Here is a less long string', &lt;/xsl:text&gt;
    &lt;xsl:text&gt;count(//*) mod 7, 7)="&lt;/xsl:text&gt;
    &lt;xsl:value-of 
      select="substring('Here is a less long string', count(//*) mod 7, 7)"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;   substring(/test/question[1]/text, 3, 7)="&lt;/xsl:text&gt;
    &lt;xsl:value-of select="substring(//*, 3, 7)"/&gt;
    &lt;xsl:text&gt;"&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre></span>
<p>Hier die Ergebnisse, die entstehen, wenn der XSLT-Prozessor Saxon verwendet wird:</p>
<span class="PROGRAMLISTING"><pre>

Tests of the substring() function:

   substring('Now is the time', 4)=" is the time"
   substring('Now is the time', 4, 6)=" is th"
   substring('Now is the time', 4, -6)="
   substring('Now is the time', -3, 6)="No"
   substring('Now is the time', 54, 6)="

   count(//*)=10
   substring('Here is a really long string', count(//*))=" really long string"
   substring('Here is a less long string', count(//*) mod 7, 7)="re is a"
   substring(/test/question[1]/text, 3, 7)=" This i"
</pre></span>
<p>Wird dieselbe Transformation mit Xalan durchgef&uuml;hrt, entsteht ein Laufzeitfehler:</p>
<span class="PROGRAMLISTING"><pre>
file:///D:/O'Reilly/XSLT/bookSamples/AppendixC/substringfunction.xsl; Line 26;
  Column 65;
Tests of the substring() function:

<!--<?troff .Nd 10?>-->
   substring('Now is the time', 4)=" is the time"
   substring('Now is the time', 4, 6)=" is th"
   substring('Now is the time', 4, -6)="
XSLT Error (javax.xml.transform.TransformerException): String index out of range
: -3</pre></span>
<p>Die momentanen Versionen von XT, Saxon und dem Oracle-Prozessor lieferten korrekte Ergebnisse; sowohl Xalan als auch Microsofts XSLT-Tools erzeugten eine Laufzeit-Exception. Die Konsequenz daraus ist, dass Sie k&uuml;nftig vern&uuml;nftige Argumente f&uuml;r die Funktion <span class="LITERAL">substring()</span> verwenden und somit alle Implementierungen einsetzen k&ouml;nnen. </p>
</td>
</tr>
</table>
</div>
</body>
</html>
