如何在XSLT中声明和迭代数组?

vic*_*ugo 10 arrays iteration xslt drop-down-menu

我的要求是 - 使用XSLT-显示一个包含US状态的下拉列表,并在XML中声明的一个特定的打印 'selected'将使用我的样式表.

我正在考虑使用状态声明一个数组并迭代它但我不知道该怎么做.

注意:欢迎更多的想法;)

Eva*_*enz 12

一种方法是将状态数据嵌入样式表本身,并使用document('')如下方式访问样式表文档:

<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>
Run Code Online (Sandbox Code Playgroud)

如果您有任何疑问,请告诉我.