1

我对 XSLT 很陌生。以下模板应该用于连接具有相同 item_group_id 的许多产品的颜色。

<xsl:template name="find-colors" mode="concat">
  <xsl:param name="param_item_group_id" />
  <xsl:param name="products" />
  <xsl:for-each select="$products/product">
    <xsl:if test="item_group_id = $param_item_group_id">
       <xsl:value-of select="concat($param_item_group_id,\'-\',item_group_id,\'-\', color,\', \')" />
    </xsl:if>
  </xsl:for-each>
</xsl:template>

在调用模板中,item_group_id 设置为 $param_item_group_id。串联本身效果很好,但我似乎无法找到一种方法

<xsl:if test="item_group_id = $param_item_group_id">

工作。该语句始终为真,因此在此模板的输出中也存在这些类型的字符串

354655-354655-green, 54655-354632-red, 354655-354632-green

它应该只在哪里

354655-354655-green

谢谢,

彼得

编辑:

最终解决方案(改编自 JLRishe 建议)是使用

<xsl:if test="item_group_id[position()] = $param_item_group_id[position()]">

映射两个数组的值。

4

1 回答 1

2

我可以看到两种可能的解释:

  1. $param_item_group_id包含多个节点。
  2. product多个 s item_group_id

例如,如果$param_item_group_id看起来像这样:

[1] - <someNode>54655<someNode>
[2] - <someNode>354632<someNode>

你有一个product这样的:

<product>
   <item_group_id>354632</item_group_id>
   <name>widgets</name>
   <color>periwinkle</color>
</product>

The comparison would evaluate to true (because they have a value in common), and the value-of would produce 54644-354632-periwinkle (because "54655" is the first node value in $param_item_group_id). In the opposite case (one node in $param_item_group_id and more than one item_group_id in a product), the same thing could happen.

That's only conjecture. I'd need to see your source XML and more of the XSLT to provide a more confident answer or help you remedy the issue.

I would expect this to prevent those incongruous outputs:

<xsl:if test="item_group_id[1] = $param_item_group_id[1]">

but I suspect there is a deeper underlying issue that this would not fix.

To clarify your questions from the comments:

The equality operator in XPath works differently from equality in a lot of other languages. Supposing A or B or both are node-sets, the expression A = B will be true if any of the values in A equal any of the values in B. Hence my description above.

Regarding this question:

correct me if I'm wring, but this should not display if it was an array, right?

It would display. Passing a nodeset to concat() converts it to a string value, and the string value of a nodeset is the string value of its first node. If the value of the first node in $param_item_group_id is 54644, then that is what would display.

I think that $param_item_group_id is exhibiting some behavior that you haven't pinned down and it's possible there's an issue in the way you're producing it. I'd suggest trying out the following to get a closer look at the contents of this variable:

<xsl:value-of select="concat('param_item_group_id contains ', 
                             count($param_item_group_id),
                             ' nodes.')" />
<xsl:for-each select="$param_item_group_id">
   <xsl:value-of select="concat(., ', ')" />
</xsl:for-each>
于 2013-02-01T17:58:37.800 回答