XSLT ha una funzione Split ()?
Domanda
Ho una stringa in un nodo e vorrei dividere la stringa su '?' e restituisce l'ultimo elemento dell'array.
Ad esempio, nel blocco seguente:
<a>
<xsl:attribute name="href">
/newpage.aspx?<xsl:value-of select="someNode"/>
</xsl:attribute>
Link text
</a>
Vorrei dividere il valore someNode
.
Modifica: Ecco il VB.Net che utilizzo per caricare l'Xsl per la mia pagina Asp.Net:
Dim xslDocPath As String = HttpContext.Current.Server.MapPath("~/App_Data/someXslt.xsl")
Dim myXsltSettings As New XsltSettings()
Dim myXMLResolver As New XmlUrlResolver()
myXsltSettings.EnableScript = True
myXsltSettings.EnableDocumentFunction = True
myXslDoc = New XslCompiledTransform(False)
myXslDoc.Load(xslDocPath, myXsltSettings, myXMLResolver)
Dim myStringBuilder As New StringBuilder()
Dim myXmlWriter As XmlWriter = Nothing
Dim myXmlWriterSettings As New XmlWriterSettings()
myXmlWriterSettings.ConformanceLevel = ConformanceLevel.Auto
myXmlWriterSettings.Indent = True
myXmlWriterSettings.OmitXmlDeclaration = True
myXmlWriter = XmlWriter.Create(myStringBuilder, myXmlWriterSettings)
myXslDoc.Transform(xmlDoc, argumentList, myXmlWriter)
Return myStringBuilder.ToString()
Aggiornamento: ecco un esempio di divisione dell'XML su un nodo particolare
Soluzione
Utilizza un metodo ricorsivo:
<xsl:template name="output-tokens">
<xsl:param name="list" />
<xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" />
<xsl:variable name="first" select="substring-before($newlist, ' ')" />
<xsl:variable name="remaining" select="substring-after($newlist, ' ')" />
<id>
<xsl:value-of select="$first" />
</id>
<xsl:if test="$remaining">
<xsl:call-template name="output-tokens">
<xsl:with-param name="list" select="$remaining" />
</xsl:call-template>
</xsl:if>
</xsl:template>
Altri suggerimenti
Se puoi usare XSLT 2.0 o versioni successive, puoi usare tokenize (stringa, separatore)
:
tokenize("XPath is fun", "\s+")
Result: ("XPath", "is", "fun")
Vedi riferimento funzione XPath w3schools .
Per impostazione predefinita, .NET non supporta XSLT 2.0, per non parlare di XSLT 3.0. Gli unici processori 2.0+ noti per .NET sono Saxon per .NET con IKVM , Exselt , un processore .NET XSLT 3.0 attualmente in versione beta e XMLPrime processore XSLT 2.0.
Ho finito per utilizzare la sottostringa-dopo ()
. Ecco cosa ha funzionato per me:
<a>
<xsl:attribute name="href">
/newpage.aspx?<xsl:value-of select="substring-after(someNode, '?')"/>
</xsl:attribute>
Link text
</a>
Anche dopo aver impostato la versione del mio XSLT su 2.0, ho ancora " 'tokenize ()' è una funzione XSLT sconosciuta.
" errore durante il tentativo di utilizzare tokenize ()
.
Aggiunta di un'altra possibilità, se il motore del modello supporta EXSLT , è possibile utilizzare tokenize () da quello.
Ad esempio:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
...
<a>
<xsl:attribute name="href">
<xsl:text>/newpage.aspx?</xsl:text>
<xsl:value-of select="str:tokenize(someNode)[2]"/>
</xsl:attribute>
</a>
...
</xsl:stylesheet>
Purtroppo .NET non supporta XSLT 2.0. Sono abbastanza sicuro che supporti EXSLT, che ha una split () . Microsoft ha una pagina precedente sulla sua implementazione di EXSLT.
Puoi scrivere un modello usando le funzioni string-before
e string-after
e usarlo attraverso. Ho scritto un blog su questo.
Alla fine è arrivato un modello xslt che avrebbe diviso una stringa delimitata in sottostringhe. Non pretendo che sia lo script più intelligente, ma risolve sicuramente il mio problema.
foglio di stile:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Paths/Item">
<xsl:call-template name="SplitText">
<xsl:with-param name="inputString" select="Path"/>
<xsl:with-param name="delimiter" select="Delimiter"/>
</xsl:call-template>
<br/>
</xsl:for-each>
</xsl:template>
<xsl:template name="SplitText">
<xsl:param name="inputString"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($inputString, $delimiter)">
<xsl:value-of select="substring-before($inputString,$delimiter)"/>
<xsl:text disable-output-escaping = "no"> </xsl:text>
<xsl:call-template name="SplitText">
<xsl:with-param name="inputString" select="substring-after($inputString,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$inputString = ''">
<xsl:text></xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$inputString"/>
<xsl:text> </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
File XML (da trasformare):
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="textSpliter.xslt"?>
<Paths>
<Item>
<Path>C:\ProgramFiles\SomeWierdSoftware</Path>
<Delimiter>\</Delimiter>
</Item>
</Paths>
XSLT 1.0 non ha una funzione split di per sé, ma potresti potenzialmente ottenere quello che stai cercando di fare con le funzioni substring-before e substring-after.
In alternativa, se si utilizza un motore Microsoft XSLT, è possibile utilizzare C # in linea.
Solo per la cronaca, se lo stai facendo con 1.0 e hai davvero bisogno di una divisione / tokenise, hai bisogno di estensioni xslt .