如何在模板匹配xslt中使用变量

How to use variable in template match xslt

我需要在 xslt 的模板匹配中使用变量,但我将模板匹配转换为变量。我遇到语法错误。

这是我的原始 xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
<xsl:template match="/name/name[not(telephoneNav/detail/action = 'A') and not(telephoneNav/detail/action = 'S')]"/>
<xsl:template match="detail[not(action = 'A') and not(action = 'S')]"/>
</xsl:stylesheet>

这是我的xslt,已经在模板匹配中转换为变量。

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
      <xsl:variable name="actionXpath1" select="'/name/name[not(telephoneNav/detail/action = &apos;A&apos;) and not(telephoneNav/detail/action = &apos;S&apos;)]'" />
      <xsl:variable name="actionXpath2" select="'detail[not(action = &apos;A&apos;) and not(action = &apos;S&apos;)]'" />
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
      <xsl:template match="$actionXpath1"/>
      <xsl:template match="$actionXpath2"/>
    </xsl:stylesheet>

像这样https://xsltfiddle.liberty-development.net/3MvmXiw

在 XSLT 3 中使用静态参数和 _match 作为影子属性,您可以使用

<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">
  <xsl:param name="actionXpath1" static="yes" select="'/name/name[not(telephoneNav/detail/action = &quot;A&quot;) and not(telephoneNav/detail/action = &quot;S&quot;)]'" />
  <xsl:param name="actionXpath2" static="yes" select="'detail[not(action = &quot;A&quot;) and not(action = &quot;S&quot;)]'" />
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template _match="{$actionXpath1}"/>
  <xsl:template _match="{$actionXpath2}"/>
</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/3MvmXiw/1

因此您需要使用 XSLT 3 处理器,并且需要以允许设置静态参数的方式使用它,即使用专用于 XSLT 3 的 API 来支持在样式表之前设置此类参数已编译。