Un modo per farlo è quello di incorporare i dati relativi allo stato nel foglio di stile in sé, e accedere al documento foglio di stile utilizzando document('')
, come segue:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="whatever"
exclude-result-prefixes="my">
<xsl:output indent="yes"/>
<!-- The value of the state you want to select, supplied in the input XML -->
<xsl:variable name="selected-state" select="/xpath/to/state/value"/>
<!-- You have to use a namespace, or the XSLT processor will complain -->
<my:states>
<option>Alabama</option>
<option>Alaska</option>
<!-- ... -->
<option>Wisconsin</option>
<option>Wyoming</option>
</my:states>
<xsl:template match="/">
<!-- rest of HTML -->
<select name="state">
<!-- Access the embedded document as an internal "config" file -->
<xsl:apply-templates select="document('')/*/my:states/option"/>
</select>
<!-- rest of HTML -->
</xsl:template>
<!-- Copy each option -->
<xsl:template match="option">
<xsl:copy>
<!-- Add selected="selected" if this is the one -->
<xsl:if test=". = $selected-state">
<xsl:attribute name="selected">selected</xsl:attribute>
</xsl:if>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
fatemi sapere se avete domande.
fonte
2009-05-22 23:00:30
Non sarebbe anche possibile inserire il nodo my: states all'interno di una dichiarazione di variabile e utilizzare questa variabile nell'espressione select? –
In XSLT 2.0, sì. In XSLT 1.0, dovresti utilizzare una funzione di estensione, come exsl: node-set() o msxsl: node-set(). Il documento ('') soluzione non richiede né. –