Processing stacks of parameters using 'for-each' in XSL?

淺唱寂寞╮ 提交于 2019-12-02 08:23:05

The problem you are having is one of context; where you are currently positioned in the XML. Consider your xsl:for-each loop

<xsl:for-each select="PLMXML/Organisation/UserData/UserValue/UserList/Item/@value">

You are looping over @value attributes, meaning within the loop you are positioned on a @value attribute, and consequently any relative xpath expressions will be relative to that. When you do your xsl:call-template you will still be positioned on the @value attribute, and so all the xpath expressions in the xsl:param statements will be relative to that.

<xsl:param name="memberRef" 
     select="PLMXML/OrganisationMember[@id=$memberId]/@memberRef"/>  

That is to say, the select statement is looking for a PLMXML element underneath the current @value, which clearly does not exist!

The reason the first parameter, memberId, works is because you pass an explicit value to that, and so that will be used instead of the default value in the select attribute. All the other parameters will not work because they will use the default value.

The solution is, probably, is to make all your parameters have absolute expressions, which means prefixing them with a /.

<xsl:param name="memberRef" 
     select="/PLMXML/OrganisationMember[@id=$memberId]/@memberRef"/> 

The / represents the top level document node, and so the xpath expression will then be relative to that, and not the current node.

Note, you might consider using xsl:key in this instance to look-up such elements. For example

<xsl:key name="OrganisationMember" match="OrganisationMember" use="@id" />

Then the param statement becomes this:

<xsl:param name="memberRef" select="key('OrganisationMember', $memberId)/@memberRef"/> 

As an aside, it is considered an error to have two templates of equal priority matching the same thing. So, your first 'named' template should probably have the match attribute removed. i.e. Instead of <xsl:template match="/" name="test">, do this

 <xsl:template name="test">
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!