Come si divide una stringa in base ad un separatore?xslt ha una funzione split()?
Data una stringa Topic1,Topic2,Topic3
, voglio dividere la stringa in base a ,
per generare:
Topic1 Topic2 Topic3
Come si divide una stringa in base ad un separatore?xslt ha una funzione split()?
Data una stringa Topic1,Topic2,Topic3
, voglio dividere la stringa in base a ,
per generare:
Topic1 Topic2 Topic3
In XSLT 1.0 è necessario creare un modello ricorsivo. Questo foglio di stile:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="','"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<item>
<xsl:value-of select="normalize-space($text)"/>
</item>
</xsl:when>
<xsl:otherwise>
<item>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</item>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
ingresso:
<root>
<text>Item1, Item2, Item3</text>
</root>
uscita:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
In XSLT 2.0 è dotata della funzione tokenize()
nucleo. Quindi, questo foglio di stile:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="text/text()" name="tokenize">
<xsl:param name="separator" select="','"/>
<xsl:for-each select="tokenize(.,$separator)">
<item>
<xsl:value-of select="normalize-space(.)"/>
</item>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Risultato:
<root>
<text>
<item>Item1</item>
<item>Item2</item>
<item>Item3</item>
</text>
</root>
Non esiste una funzione split
, ma è possibile utilizzare un modello ricorsivo con substring-before
e substring-after
di scrivere il proprio.
Vedere l'articolo this per dettagli.
Grazie user357812. Io uso il vostro bel modello con poca personalizzazione per rendere più generico:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Main template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" mode="tokenize-children" />
</xsl:copy>
</xsl:template>
<!-- Split child nodes -->
<xsl:template match="*" mode="tokenize-children">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="*" mode="tokenize" />
</xsl:copy>
</xsl:template>
<!-- Tokenize text node of child nodes -->
<xsl:template match="*/text()" name="tokenize" mode="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="','"/>
<xsl:variable name="item" select="name(..)" />
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<xsl:element name="{$item}">
<xsl:value-of select="normalize-space($text)"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{$item}">
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</xsl:element>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
A seconda di quale processore XSL che si sta utilizzando, si può avere accesso alla funzione di estensione str:tokenize().
Quindi per dividere Topic1,Topic2,Topic3
su ,
fare;
<xsl:copy-of select="str:tokenize('Topic1,Topic2,Topic3', ',')"/>
che darà il risultato;
<token>Topic1</token>
<token>Topic2</token>
<token>Topic3</token>
XSLT 1.0
ho bisogno di una piccola variante rispetto ad altre risposte qui indicate.
ingresso:
1, 2, 3
Ouput:
1, 2 e 3
Ingresso:
uscita
Se il delimitatore è lo spazio invece di una virgola, sarebbe ancora lavorare.
ingresso:
Ouput:
1, 2 e 3
Ho appena creato un modello leggermente modificato.
<xsl:template name="tokenizeString">
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<xsl:variable name="listLength" select="string-length($list)" />
<xsl:variable name="listLengthWithoutDelimiters" select="string-length(translate($list, $delimiter,''))" />
<xsl:variable name="noOfDelimiters" select="($listLength - $listLengthWithoutDelimiters)" />
<xsl:value-of select="substring-before($list,$delimiter)"/>
<xsl:if test="$noOfDelimiters > 1">, </xsl:if>
<xsl:if test="$noOfDelimiters = 1"> and </xsl:if>
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$list = ''">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$list"/>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
Il modello può essere chiamato come di seguito quando il delimitatore è virgola
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list">1, 2, 3</xsl:with-param>
<xsl:with-param name="delimiter">
<xsl:value-of select="','" />
</xsl:with-param>
</xsl:call-template>
Il modello può essere chiamato come di seguito quando il delimitatore spazio
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list">1 2 3</xsl:with-param>
<xsl:with-param name="delimiter">
<xsl:value-of select="' '" />
</xsl:with-param>
</xsl:call-template>
possibile duplicato di [XSLT ha una funzione Split()?] (http://stackoverflow.com/questions/136500/does-xslt-have-a-split-function) –