繁体   English   中英

XSLT 参数的使用;<xsl:param> &amp;<xsl:with-param></xsl:with-param></xsl:param>

[英]Usage of XSLT Params; <xsl:param> & <xsl:with-param>

请解释我如何使用最好的 XSLT 参数。 根据<xsl:param> & <xsl:with-param>

样本 LOC:

<xsl:call-template name="ABC">
    <xsl:with-param name="title" />
</xsl:call-template>

请向我解释如何最好地使用 XSLT 参数。 <xsl:param><xsl:with-param>

<xsl:param>可以在全局级别的任何地方指定(作为xsl:stylesheet的子项),或者如果它在模板中,它必须是它的子项并且它必须在xsl:template任何非xsl:param子项之前.

这是允许模板或整个转换(在全局xsl:param情况下)分别从模板或整个转换的调用者/发起者接收不同数据的工具。

在模板/转换的调用者/发起者一侧,使用xsl:with-param指令传递xsl:with-param 它可以是xsl:apply-templatesxsl:call-template的子代。

xsl:paramxsl:with-paramname属性是强制性的。 它标识参数。

xsl:with-param的 select 属性可用于指定任何XPath 表达式,其计算结果将传递给被调用/应用的模板。

或者,可以在xsl:with-param的内容(正文)中指定该值。

xsl:with-param必须具有select属性或正文。 但不是他们两个。

xsl:param也可以有一个选择属性或主体。 在这种情况下,这些指定参数的默认值,如果调用者没有指定具有此名称的参数,则使用它。

最后,这里有一个完整的例子来说明大多数这些概念

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:param name="pTarget" select="'love'"/>
 <xsl:param name="pReplacement" select="'like'"/>

 <xsl:template match="/*">
  <xsl:call-template name="replace">
   <xsl:with-param name="pPattern" select="$pTarget"/>
   <xsl:with-param name="pRep" select="$pReplacement"/>
  </xsl:call-template>

  <xsl:text>&#xA;</xsl:text>

  <xsl:call-template name="replace"/>

  <xsl:text>&#xA;</xsl:text>

  <xsl:apply-templates select="text()">
   <xsl:with-param name="pPattern" select="$pTarget"/>
   <xsl:with-param name="pRep" select="'adore'"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="text()" name="replace">
   <xsl:param name="pText" select="."/>
   <xsl:param name="pPattern" select="'hate'"/>
   <xsl:param name="pRep" select="'disapprove'"/>

   <xsl:if test="string-length($pText) >0">
       <xsl:choose>
        <xsl:when test="not(contains($pText, $pPattern))">
          <xsl:value-of select="$pText"/>
        </xsl:when>
        <xsl:otherwise>
         <xsl:value-of select="substring-before($pText, $pPattern)"/>
         <xsl:value-of select="$pRep"/>

         <xsl:call-template name="replace">
           <xsl:with-param name="pPattern" select="$pPattern"/>
           <xsl:with-param name="pRep" select="$pRep"/>
           <xsl:with-param name="pText" select=
            "substring-after($pText, $pPattern)"/>
         </xsl:call-template>
        </xsl:otherwise>
       </xsl:choose>
   </xsl:if>
 </xsl:template>
</xsl:stylesheet>

当应用于此 XML 文档时...

<t>Sports stars we really love, love to hate, hate</t>

……结果……

Sports stars we really like, like to hate, hate
Sports stars we really love, love to disapprove, disapprove
Sports stars we really adore, adore to hate, hate

说明

  1. replace模板被调用两次。 在这两个调用中都省略了pText参数。 其默认值由被调用的模板使用。

  2. 第一个调用提供了模式和替换参数,所以"love""like"替换。

  3. 请注意,传递了全局参数$pTarget$pReplacement的值。 如果转换的发起者决定为这些全局参数传递其他值(不是此代码中使用的默认值),则这些值将传递给replace模板,而不是默认值"love""like"

  4. 第二个调用根本不提供任何参数值,因此使用replace模板中的所有默认值——字符串"hate"替换为字符串"disapprove"

  5. 请注意, replace模板会递归调用自身,因此所有出现的模式都将被替换替换。

  6. 此外,递归调用的pText参数值不是静态的,而是动态计算的。

  7. 第三次从外部启动replace模板是通过xsl:apply-templates 这里我们还展示了一个模板可以同时具有matchname属性,并且可以使用xsl:apply-templatesxsl:call-template来启动这样的模板是可能的。

它用于传递在另一个模板中定义的参数:

<xsl:param name="globalParam"></xsl:param>

<xsl:call-template name="ABC">
  <xsl:with-param name="title" select="'A Title'" />
</xsl:call-template>

<xsl:template name="ABC">
  <xsl:param name="title"/>
  <xsl:value-of select="$title" />
  <xsl:value-of select="$globalParam" />
</xsl:template>

我有以下使用 xsl-template 的代码,如下所示:

<xsl:template name="closureRecords">

    <xsl:param name="recordStackToBeGeneratedLocal" /> <!-- it is java stack contains list of values which i want to procees it-->

    <xsl:if test="not(stack:empty($recordStackToBeGeneratedLocal))">


                        <xsl:variable name="dummstack"  select="stack:push($recordStackToBeGeneratedLocal, 'helpppppp')"/>
                         pop   recordStackToBeGenerated : <xsl:value-of select="stack:pop($recordStackToBeGeneratedLocal)"/> --> does not peek the stack .. prints all values
                         pop1   recordStackToBeGenerated : <xsl:value-of select="stack:peek($recordStackToBeGeneratedLocal)"/> --> does not peek the stack .. prints all values
                           recordStackToBeGenerated : <xsl:value-of select="(stack:empty($recordStackToBeGeneratedLocal))"/>  --> false

    </xsl:if>

</xsl:模板>

请让我知道为什么 push/pop/peek 操作没有做任何事情。 让我知道我是否必须使用模板参数并使用不同的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM