【问题标题】:xslt functions to stop repeated codexslt 函数停止重复代码
【发布时间】:2013-02-05 09:56:57
【问题描述】:

您好,我是 xslt 的初学者,并且刚刚开始使用它,因为我正在使用 Umbraco 构建网站。我想知道是否有任何方法可以在 xslt 中创建函数,所以我不必重复同样的事情。看了几个网站,没看懂

我的代码是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet 
  version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:msxml="urn:schemas-microsoft-com:xslt" 
  xmlns:umbraco.library="urn:umbraco.library"
  exclude-result-prefixes="msxml umbraco.library">

<xsl:output method="xml" omit-xml-declaration="yes"/>

<xsl:param name="currentPage"/>

<xsl:template match="/">

<xsl:variable name="items" select="$currentPage/ancestor-or-self::* [@isDoc and @level = 1]/* [@isDoc and string(umbracoNaviHide) != '1']"/>

<!-- The fun starts here -->

<xsl:if test="count($items) &gt; 0">
<ul id="SubNav" class="level{@level}">
  <xsl:for-each select="$items">
  <li>
    <a href="{umbraco.library:NiceUrl(@id)}">
      <xsl:value-of select="@nodeName"/>
    </a>
    <xsl:if test="count(./child::*[@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">
        <ul class="level{@level}">
        <xsl:for-each select="./child::*[@isDoc and string(umbracoNaviHide) != '1']">
          <li>
            <a href="{umbraco.library:NiceUrl(@id)}">
              <xsl:value-of select="@nodeName"/>
            </a>
            <xsl:if test="count(./child::*[@isDoc and string(umbracoNaviHide) != '1']) &gt; 0">
                <ul class="level{@level}">
                <xsl:for-each select="./child::*[@isDoc and string(umbracoNaviHide) != '1']">
                  <li>
                    <a href="{umbraco.library:NiceUrl(@id)}">
                      <xsl:value-of select="@nodeName"/>
                    </a>
                  </li>
                </xsl:for-each>
                </ul>
            </xsl:if>
          </li>
        </xsl:for-each>
        </ul>
    </xsl:if>
  </li>
  </xsl:for-each>
</ul>
</xsl:if>

</xsl:template>

</xsl:stylesheet>

正如您所看到的 foreach 节点,我正在重用相同的代码来列出子节点,所以我想知道是否可以将其拉出到一个函数中,这样我就不必为我需要的每个级别的子节点嵌套相同的代码

【问题讨论】:

    标签: xslt umbraco


    【解决方案1】:

    XSLT 的理念是使用与 XML 结构中的某些节点匹配的模板。您甚至可以使用“模式”应用具有不同处理指令的完全相同的模板。

    我不知道您的 XML 结构,但我认为这是可行的方法。在 stackoverflow 中搜索也提供了信息:https://stackoverflow.com/questions/tagged/templates+xslt

    【讨论】:

    • 感谢这些信息,我能够确定我需要另一个模板并从中找到了解决方案
    • @Pete:不客气。听起来不错。很高兴我能帮上忙。最好的问候,彼得
    【解决方案2】:

    XSLT 2.0 允许您编写可以从代码中的 XPath 表达式调用的函数。例如,您可以定义

    <xsl:function name="f:isRelevant" as="xs:boolean">
      <xsl:param name="node" as="element()"/>
      <xsl:sequence 
        select="count($node/child::*[@isDoc and string(umbracoNaviHide) != '1'])"/>
    </xsl:function>
    

    然后你可以写,例如

    <xsl:if test="f:isRelevant(.)">...</xsl:if>
    

    如果您所在的平台限制您使用 XSLT 1.0,那么您将不得不忍受更冗长的代码。您可以对常见的 XSLT 代码块使用命名模板和 xsl:call-template 指令,但它们不如函数方便,例如,您不能在 xsl:if 的测试表达式中调用它们例子。

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 2013-02-01
      • 2011-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      • 2018-10-10
      • 2011-03-24
      相关资源
      最近更新 更多