【问题标题】:XSLT: Simulating a template match inside a variableXSLT:在变量中模拟模板匹配
【发布时间】:2013-03-12 15:35:34
【问题描述】:

我想知道如何根据下面的代码在变量中模拟模板mach。

我想将这个 XSLT 代码的输出存储在一个变量中,稍后在脚本中使用它:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

我现在可以通过使用 2 个 XSLT 脚本来实现这一点,但我只想使用一个脚本。

XSLT 脚本应该给出相同的输出,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:variable name="InputNew">
  <xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>
  </xsl:variable>

  <xsl:template match="/">
     <xsl:copy-of select="$InputNew"/>
  </xsl:template>
</xsl:stylesheet>

我知道我不能在变量中使用模板匹配,我只能使用 call-template 但我无法为此编写正确的 XSLT 代码。

编辑:这是工作脚本:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exclude-result-prefixes="xsl soap xsi">
<xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes" omit-xml-declaration="yes"/>
   <xsl:strip-space elements="*"/>

    <xsl:template match ="*" mode="stripNs">
        <xsl:element name ="{local-name()}" >
            <xsl:apply-templates select ="@* | node()" mode="stripNs"/>
        </xsl:element>
    </xsl:template>


    <xsl:variable name="noNamespaces">
       <xsl:apply-templates select="soap:Envelope/soap:Body/*" mode="stripNs" />
    </xsl:variable>

    <xsl:template match="/">
       <!-- Now I can do what I want with the variable $noNamespaces -->
       <xsl:copy-of select="$noNamespaces"/>
    </xsl:template>

</xsl:stylesheet>

【问题讨论】:

  • 您使用的是什么 XSLT 处理器?在一些 XSLT 处理器中,可以在变量中捕获 XML 树,然后使用 node-set() 函数与该变量的内容进行交互。
  • 您好,感谢您的快速反馈!我正在使用撒克逊人。

标签: xml xslt


【解决方案1】:

如果您使用的是 Saxon,您应该可以使用 exslt:node-set() 函数来执行此操作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:exslt="http://exslt.org/common">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="*" mode="stripNs">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()" mode="stripNs"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*" mode="stripNs">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="/">
    <!-- Apply the stripNs templates and capture the result in a variable -->
    <xsl:variable name="noNamespaces">
      <xsl:apply-templates select="*" mode="stripNs" />
    </xsl:variable>

    <!-- Now use the variable -->
    <xsl:apply-templates select="exslt:node-set($noNamespaces)/*" />
  </xsl:template>
</xsl:stylesheet>

再一次,如果您使用的是 Saxon,您也可以使用 XSLT 2.0 并使用与上述相同的方法,但无需在 $noNamespaces 周围使用 exslt:node-set():

<!-- Now use the variable -->
<xsl:apply-templates select="$noNamespaces/*" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-18
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2019-06-18
    • 2017-01-14
    相关资源
    最近更新 更多