微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

调用包含的模板时“If-element”没有反应

如何解决调用包含的模板时“If-element”没有反应

我希望构建一个简单的逻辑,让用户定义应该调用哪些模板。该代码说明了一个最小化的示例。当有许多模板时,该设置非常有用,例如我正在构建的输出大约是 2.600 行 XHTML 代码。那么能够排除几个模板而只关注其中的一些模板就很好了。

我已经成功创建了上述设置(以前),并将 XML 作为源文件和模块化代码。我怀疑这是我将 JSON 作为源文件的设置以及导致问题的代码改编。

下面的代码应该允许用户将变量“build-with-books”从 0 切换到 1,如果设置为 1,“if”元素应该调用包含的模板。

我确信有许多“更聪明”的方法可以解决我的需求。目前我只是想了解为什么我的代码不遵循打开/关闭元素构建的逻辑。

XSLT fiddle关闭,所以我只粘贴下面的代码

数据:

<data>
{
  "books": {
    "Wonderland": 43
  },"beverage": {
    "Falcon": 12
  }
}
</data>

principal.xsl:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ix="http://www.example.com/1"
  xmlns:xbrli="http://www.example.com/2"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  exclude-result-prefixes="fn"
  expand-text="yes"
>

  <xsl:output method="xml" indent="yes"/>

  <!-- Block all data that has no user defined template -->
  <xsl:mode on-no-match="shallow-skip"/>

  <!-- Includes -->
  <xsl:include href="books.xsl"/>

  <!-- Module selector -->
  <xsl:variable name="build-with-books">0</xsl:variable>

  <!-- Attribute-sets -->
  <xsl:attribute-set name="books">
    <xsl:attribute name="category">Adventure</xsl:attribute>
  </xsl:attribute-set>

  <!-- Main template -->

  <xsl:template match="data">

      <!-- Parse JSON to XML,results in XML map -->
      <xbrli:xbrl>
        <xsl:apply-templates select="json-to-xml(.)/*"/>
      </xbrli:xbrl>

      <!-- Call template -->    
      <xsl:if test=" $build-with-books = '1' ">
        <xsl:call-template name="books"/>
      </xsl:if>

  </xsl:template>

</xsl:transform>

支持模块:books.xsl

<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform version="3.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ix="http://www.example.com/1"
  xmlns:xbrli="http://www.example.com/2"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  exclude-result-prefixes="fn"
  expand-text="yes"
>

<xsl:output method="xml" indent="yes"/>

<!-- Create elements  -->

<xsl:template name="books" match="*[@key = 'books']//*[@key and not(*)]">
    <xsl:element name="ix:{@key}" use-attribute-sets="books">{.}</xsl:element>
</xsl:template>

</xsl:transform>

结果:

<?xml version="1.0" encoding="UTF-8"?>
<xbrli:xbrl xmlns:ix="http://www.example.com/1" xmlns:xbrli="http://www.example.com/2">
   <ix:Wonderland category="Adventure">43</ix:Wonderland>
</xbrli:xbrl>

如果变量“build-with-books”设置为 0 的预期结果

<?xml version="1.0" encoding="UTF-8"?>
<xbrli:xbrl xmlns:ix="http://www.example.com/1" xmlns:xbrli="http://www.example.com/2">
</xbrli:xbrl>

如果变量“build-with-books”设置为 1 的预期结果

<?xml version="1.0" encoding="UTF-8"?>
<xbrli:xbrl xmlns:ix="http://www.example.com/1" xmlns:xbrli="http://www.example.com/2">
   <ix:Wonderland category="Adventure">43</ix:Wonderland>
</xbrli:xbrl>

解决方法

开始

从principal.xsl中删除这部分:(调用模板使用当前上下文,这仍然是你对数据的匹配)

  <!-- Call template -->    
  <xsl:if test=" $build-with-books = '1' ">
    <xsl:call-template name="books"/>
  </xsl:if>

并从

中删除@name属性
<xsl:template name="books" match="*[@key = 'books']//*[@key and not(*)]">

选项:1

将books.xls 中的匹配模板更改为如下所示(它将直接使用您的全局 $build-with-books

  <xsl:template match="*[$build-with-books='1'][@key = 'books']//*[@key and not(*)]">
    <xsl:element name="ix:{@key}" use-attribute-sets="books">{.}</xsl:element>
  </xsl:template>

选项:2

在您的 principal.xsl 中使用 xsl:next-match:(它将首先使用具有更高优先级的模板来检查全局 $build-with-books

添加:

  <xsl:template match="*[@key = 'books']" priority="10">
    <xsl:if test="$build-with-books='1'">
      <xsl:next-match/>
    </xsl:if>
  </xsl:template>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。