简体   繁体   English

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

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

Please explain me how best XSLT param can be used.请解释我如何使用最好的 XSLT 参数。 in terms of <xsl:param> & <xsl:with-param>根据<xsl:param> & <xsl:with-param>

Sample LOC:样本 LOC:

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

Please explain me how best XSLT param can be used.请向我解释如何最好地使用 XSLT 参数。 in terms of <xsl:param> & <xsl:with-param><xsl:param><xsl:with-param>

<xsl:param> can be specified at the global level anywhere (as a child of xsl:stylesheet ) or if it is within a template, it must be its child and it must precede any non- xsl:param child of xsl:template . <xsl:param>可以在全局级别的任何地方指定(作为xsl:stylesheet的子项),或者如果它在模板中,它必须是它的子项并且它必须在xsl:template任何非xsl:param子项之前.

This is the facility that allows a template or the whole transformation (in case of a global xsl:param ) to receive varying data from the caller/initiator of the template or of the whole transformation, respectively.这是允许模板或整个转换(在全局xsl:param情况下)分别从模板或整个转换的调用者/发起者接收不同数据的工具。

On the side of the caller/initiator of the template/transformation, parameters are passed by using an xsl:with-param instruction.在模板/转换的调用者/发起者一侧,使用xsl:with-param指令传递xsl:with-param it can be a child of xsl:apply-templates or xsl:call-template .它可以是xsl:apply-templatesxsl:call-template的子代。

The name attribute of either xsl:param or xsl:with-param is mandatory. xsl:paramxsl:with-paramname属性是强制性的。 It identifies the parameter.它标识参数。

The select attribute of xsl:with-param may be used to specify any XPath expression, the result of whose evaluation is passed to the called/applied template. xsl:with-param的 select 属性可用于指定任何XPath 表达式,其计算结果将传递给被调用/应用的模板。

Alternatively, the value can be specified in the content (body) of xsl:with-param .或者,可以在xsl:with-param的内容(正文)中指定该值。

xsl:with-param must have either a select attribute or a body. xsl:with-param必须具有select属性或正文。 but not both of them.但不是他们两个。

An xsl:param can also have a select attribute or body. xsl:param也可以有一个选择属性或主体。 In this case, these specify the default value of the parameter and it is used if no parameter with this name has been specified by the caller.在这种情况下,这些指定参数的默认值,如果调用者没有指定具有此名称的参数,则使用它。

Finally, here is a complete example illustrating most of these concepts :最后,这里有一个完整的例子来说明大多数这些概念

<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>

When applied on this XML document...当应用于此 XML 文档时...

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

...the result is... ……结果……

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

Explanation :说明

  1. The replace template is called twice. replace模板被调用两次。 In both calls the pText parameter is omitted.在这两个调用中都省略了pText参数。 Its default value is used by the called template.其默认值由被调用的模板使用。

  2. The first call provides the pattern and replacement parameters, so "love" is replaced by "like" .第一个调用提供了模式和替换参数,所以"love""like"替换。

  3. Do note that the values of the global parameters $pTarget and $pReplacement are passed through.请注意,传递了全局参数$pTarget$pReplacement的值。 If the initiator of the transformation decides to pass other values (not the defaults that are used in this code) for these global parameters, these values will be passed to the replace template and not the defaults "love" and "like" .如果转换的发起者决定为这些全局参数传递其他值(不是此代码中使用的默认值),则这些值将传递给replace模板,而不是默认值"love""like"

  4. The second call doesn't provide any parameter values at all, so all defaults in the replace template are used -- the string "hate" is replaced by the string "disapprove" .第二个调用根本不提供任何参数值,因此使用replace模板中的所有默认值——字符串"hate"替换为字符串"disapprove"

  5. Note that the replace template calls itself recursively, so that all occurrences of the pattern are replaced by the replacement.请注意, replace模板会递归调用自身,因此所有出现的模式都将被替换替换。

  6. Also, the values of the pText parameter of the recursive calls aren't static, but are dynamically calculated.此外,递归调用的pText参数值不是静态的,而是动态计算的。

  7. The third time the replace template is initiated from outside is via xsl:apply-templates .第三次从外部启动replace模板是通过xsl:apply-templates Here we also show that a template can have both a match and a name attribute at the same time and it is possible that such a template can be initiated both using xsl:apply-templates and xsl:call-template .这里我们还展示了一个模板可以同时具有matchname属性,并且可以使用xsl:apply-templatesxsl:call-template来启动这样的模板是可能的。

It's used to pass a param defined in another 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>

I have the below code for using xsl-template as below:我有以下使用 xsl-template 的代码,如下所示:

<xsl:template name="closingRecords"> <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:template> </xsl:模板>

Please let me know why the push/pop/peek operation is not doing anything.请让我知道为什么 push/pop/peek 操作没有做任何事情。 Let me know if I have to use the template parameter and use a different way.让我知道我是否必须使用模板参数并使用不同的方式。

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

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