【问题标题】:xslt: Use variables in select and matchxslt:在选择和匹配中使用变量
【发布时间】:2016-12-14 06:53:59
【问题描述】:

我需要指定从 html 文件到文本文件的输出顺序。因此我使用 xsl:apply-templates 选择方法。 它工作正常,但为了微调不同节点的输出,我需要一个相应的模板,而不仅仅是一个通用模板。这也可以,但我需要在模板的匹配模式中重复选择模式。

我喜欢定义一个保存模式的变量,所以它只需要定义一次。 下面是我的简化样式表和简化的 html,它们不起作用,但给出了我想要完成的想法。 是否可以使用这样的变量?如果需要,我可以同时使用 xslt 1.0 和 2.0。

<xsl:stylesheet ...>
    ...

  <xsl:variable name="first">div[@class='one']</xsl:variable>
  <xsl:variable name="second">div[@class='two']</xsl:variable>

  <xsl:template match="/*">
    <xsl:apply-templates select="//$first"/>                                        
    <xsl:apply-templates select="//$second"/>                                       
    ...
  </xsl:template>

  <xsl:template match="//$first">
    <xsl:text>Custom text for class one:</xsl:text><xsl:value-of select="text()"/>
  </xsl:template>

  <xsl:template match="//$second">
    <xsl:text>Custom text for class two:</xsl:text><xsl:value-of select="text()"/>
  </xsl:template>

</xsl:stylesheet>   

html:

...
<div class="two">text from two</div>
<div class="one">text from one </div>
...

期望的输出:

Custom text for class one: text from one
Custom text for class two: text from two

【问题讨论】:

    标签: html xslt


    【解决方案1】:

    XSLT 中的变量保存值,而不是表达式的片段。 (换句话说,XSLT 不是宏语言)。

    作为需要 XSLT 3.0 的 Martin 解决方案的替代方案,您可以考虑使用有时称为“元样式表”的东西 - 将转换作为样式表本身的预处理步骤。您甚至可以编写通用样式表以使用带有影子属性(如 _match)的 XSLT 3.0 语法,并执行 XSLT 预处理阶段以将其转换为常规 XSLT 1.0 或 2.0 语法以供执行。

    【讨论】:

      【解决方案2】:

      没有办法像在 XSLT 1 或 2 中那样使用变量。唯一的方法是编写一个样式表,生成第二个样式表并单独执行。

      在 XSLT 3 中有称为 static variables/parametersshadow attributes 的新功能可以提供帮助,或者您可以使用 transform 函数直接使用 XSLT 执行新生成的样式表,而不是使用宿主语言在单独的步骤中执行.

      但是使用 XSLT 2 你可以缩短

      <xsl:apply-templates select="//div[@class='one']"/>                                        
      <xsl:apply-templates select="//div[@class='two']"/> 
      

      <xsl:apply-templates select="//div[@class='one'], //div[@class='two']"/>
      

      为了完整起见,这里是 XSLT 3 方法,阴影属性中使用了两个静态参数:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:math="http://www.w3.org/2005/xpath-functions/math"
          exclude-result-prefixes="xs math"
          version="3.0">
      
          <xsl:param name="first" static="yes" as="xs:string" select="&quot;div[@class='one']&quot;"/>
      
          <xsl:param name="second" static="yes" as="xs:string" select="&quot;div[@class='two']&quot;"/>
      
          <xsl:template match="/*">
              <xsl:apply-templates _select="//{$first}, //{$second}"/>                                        
          </xsl:template>
      
          <xsl:template _match="{$first}">
              <xsl:text>Custom text for class one:</xsl:text><xsl:value-of select="text()"/>
          </xsl:template>
      
          <xsl:template _match="{$second}">
              <xsl:text>Custom text for class two:</xsl:text><xsl:value-of select="text()"/>
          </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        相关资源
        最近更新 更多