XSLT 参数的使用;<xsl:param>&<xsl:with-param> [英] Usage of XSLT Params; <xsl:param> & <xsl:with-param>

查看:32
本文介绍了XSLT 参数的使用;<xsl:param>&<xsl:with-param>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请解释如何最好地使用 XSLT 参数.就 & 而言

Please explain me how best XSLT param can be used. in terms of <xsl:param> & <xsl:with-param>

示例 LOC:

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

推荐答案

请解释如何最好地使用 XSLT 参数.按照 &

可以在全局级别的任何地方指定(作为 xsl:stylesheet 的子项),或者如果它在模板中,则必须是它的子级,并且必须在 xsl:template 的任何非 xsl: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 的情况下)从模板或整个转换的调用者/发起者接收不同数据的工具,分别.

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:with-param 指令传递参数.它可以是 xsl:apply-templatesxsl:call-template 的孩子.

On the side of the caller/initiator of the template/transformation, parameters are passed by using an xsl:with-param instruction. it can be a child of xsl:apply-templates or xsl:call-template.

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

The name attribute of either xsl:param or xsl:with-param is mandatory. It identifies the parameter.

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

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 的内容(正文)中指定该值.

Alternatively, the value can be specified in the content (body) of xsl:with-param.

xsl:with-param 必须具有 select 属性或正文.但不是两者都有.

xsl:with-param must have either a select attribute or a body. but not both of them.

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

An xsl:param can also have a select attribute or body. 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.

最后,这里有一个完整的例子来说明大部分概念:

<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 参数.其默认值由被调用的模板使用.

  1. The replace template is called twice. In both calls the pText parameter is omitted. Its default value is used by the called template.

第一次调用提供了模式和替换参数,所以 "love" 被替换为 "like".

The first call provides the pattern and replacement parameters, so "love" is replaced by "like".

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

Do note that the values of the global parameters $pTarget and $pReplacement are passed through. 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 模板中的所有默认值——字符串 "hate" 是替换为字符串 "disapprove".

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 模板会递归调用自身,因此所有出现的模式都会被替换替换.

Note that the replace template calls itself recursively, so that all occurrences of the pattern are replaced by the replacement.

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

Also, the values of the pText parameter of the recursive calls aren't static, but are dynamically calculated.

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

The third time the replace template is initiated from outside is via 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.

这篇关于XSLT 参数的使用;&lt;xsl:param&gt;&amp;&lt;xsl:with-param&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆