简体   繁体   English

使用xsl:param在xsl:template中选择属性

[英]use xsl:param to select attribute in xsl:template

I need to output 2 attributes of a kind of element. 我需要输出2种元素的属性。 I'd like to use only 1 template with one parameter to reduce code. 我只想使用带有一个参数的1个模板来减少代码。

XML is like : XML就像:

<myElement attribute1="x" attribute2="y" />

I'd like to call templates like this : 我想这样调用模板:

<xsl:apply-templates select="myElement">
   <xsl:with-param name="paramAttr" select="@attribute1" />
</xsl:apply-templates>

and

<xsl:apply-templates select="myElement">
  <xsl:with-param name="paramAttr" select="@attribute2" />
</xsl:apply-templates>

Problem is in the definition of the template : 问题出在模板的定义中:

<xsl:template match="myElement">
  <xsl:param name="paramAttr"/>

  <xsl:value-of select="$paramAttr" /> <!-- NOT ACCEPTED -->

</xsl:template>

I tried different things like introducing @ at different places, with concat() or {} without much success. 我尝试了不同的操作,例如在不同的地方使用concat()或{}引入@,但没有成功。 Any idea ? 任何想法 ? thank you. 谢谢。

Pass the attribute name as a string: 将属性名称作为字符串传递:

<xsl:apply-templates select="myElement">
   <xsl:with-param name="paramAttr" select="'attribute1'" />
</xsl:apply-templates>

and select the attribute based on its name in the called template: 并根据其名称在调用的模板中选择属性:

<xsl:template match="myElement">
  <xsl:param name="paramAttr"/>
  <xsl:value-of select="@*[name() = $paramAttr]" />
</xsl:template>

You haven't really explained what problem occurred with your current approach but I think it results from the rule on how xsl:with-param is evaluated, if you look at https://www.w3.org/TR/xslt-10/#element-with-param it says 您尚未真正解释当前方法所发生的问题,但我认为这是有关如何评估xsl:with-param的规则xsl:with-param ,如果您查看https://www.w3.org/TR/xslt-10 /#element-with-param它说

The current node and current node list used for computing the value specified by xsl:with-param element is the same as that used for the xsl:apply-templates or xsl:call-template element within which it occurs 用于计算xsl:with-param元素指定的值的当前节点和当前节点列表与xsl:apply-templates或xsl:call-template元素所使用的值相同

respectively XSLT 3 defines in https://www.w3.org/TR/xslt-30/#element-with-param that XSLT 3分别在https://www.w3.org/TR/xslt-30/#element-with-param中定义

The focus used for computing the value specified by the xsl:with-param element is the same as that used for its parent instruction. 用于计算xsl:with-param元素指定的值的焦点与用于其父指令的焦点相同。

That means, if the outer context/focus of your xsl:apply-templates has a myElement child you select for the apply-templates , in the xsl:with-param you need to use <xsl:with-param name="paramAttr" select="myElement/@attribute1 "/>, as the xsl:apply-templates doesn't change the context/focus for its inner xsl:with-param . 这意味着,如果xsl:apply-templates的外部上下文/焦点有一个myElement子项,您可以为apply-templates选择,则在xsl:with-param您需要使用<xsl:with-param name="paramAttr" select="myElement/@attribute1 ” />,因为xsl:apply-templates不会更改其内部xsl:with-param的上下文/焦点。 Of course using a variable with eg 当然使用带有例如变量

 <xsl:variable name="el" select="myElement"/>
 <xsl:apply-templates select="$el">
    <xsl:with-param name="paramAttr" select="$el/@attribute1"/>
 </xsl:apply-templates>

can make that approach shorter and more readable. 可以使该方法更短,更易读。 If there are several myElement elements then it is better to change the context/focus either with 如果有多个myElement元素,则最好使用以下方法更改上下文/焦点

<xsl:for-each select="myElement">
  <xsl:apply-templates select=".">
    <xsl:with-param name="paramAttr" select="@attribute1"/>
  </xsl:apply-templates>
</xsl:for-each>

or to first push the myElement element to a template where you then further process the attributes. 或先将myElement元素推myElement模板,然后在其中进一步处理属性。

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

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