XSL / Die XSLT - Struktur / XPath 3.1: Array / XPath: array:get
![]() |
![]() |
➪ Ein Array ist ein neuer Datentyp in XDM 3.1 mit umfangreicher Funktionalität, der als ein Item einer Sequenz zu betrachten ist.
Die XPath-Funktion array:get liefert jenen Wert, der an einer bestimmten Indexstelle steht (gezählt wird von 1 an).
<xsl:template match="/">
<root>
<xsl:value-of select="array:get($varray, 2)"/>
</root>
</xsl:template>
array:get($varray, 2) gibt daher das Resultat "Lotte".
<root>Lotte</root>
Besteht hingegen ein Feld in einem Array selber aus einem Array,
<xsl:template match="/">
<root>
<xsl:value-of
select="array:get([['Hugo', 'Lotte'], ['Theo']], 1)"/>
</root>
</xsl:template>
so ist in diesem Fall das Ergebnis:
<root>Hugo Lotte</root>
Etwas interessanter wird es noch, die Einzelwerte des Arrays auszugeben:
<xsl:template match="/">
<root>
<xsl:for-each select="1 to array:size($varray)">
<xsl:variable name="vpos" select="fn:position()"/>
<wert nr="{$vpos}">
<xsl:value-of select="array:get($varray, $vpos)"/>
</wert>
</xsl:for-each>
</root>
</xsl:template>
Und auch hierfür gibt es ein Ergebnis:
<root>
<wert nr="1">Hugo</wert>
<wert nr="2">Lotte</wert>
<wert nr="3">Theo</wert>
</root>
Ein anderes Beispiel mit XQuery:
declare namespace array="http://www.w3.org/2005/xpath-functions/array";
let $input :=
<a>
<b>
<c>
<ID>123</ID>
</c>
<c>
<ID>456</ID>
</c>
</b>
</a>
let $myarray := array{ ($input/b/c/ID/text()) }
return array:get($myarray, 2)
DDas Ergebnis lautet einfach:
456
wg / 31. Oktober 2020
Fragen? Anmerkungen? Tipps?
Bitte nehmen Sie Kontakt zu mir auf.
V.i.S.d.P.: Wilfried Grupe * Klus 6 * 37643 Negenborn
☎ 0151. 750 360 61 * eMail: info10@wilfried-grupe.de