为什么 for-each 不能一致地工作?

发布于 2024-10-26 20:38:20 字数 3735 浏览 0 评论 0原文

当我使用

<xsl:for-each select="plm:PLMXML/plm:ProductRevision>

如下所示的 value-of

<td><xsl:value-of select="plm:UserData/plm:UserValue[1]/@value"/></td>

时,我用 xsl 形成的网页工作得很好。但是,当我在我创建的网页中使用

<xsl:for-each select="plm:PLMXML>

and a value-of

<td>
    <xsl:value-of 
        select="plm:ProductRevision/plm:UserData/plm:UserValue[1]/@value"/>
</td>

时,不再正确形成。

在第一种情况下,htm 的输出如下:

<body>
      <h2>My CD Collection</h2>
      <table border="1">
         <tr bgcolor="#9acd33">
            <th>Category Type</th>
            <th>DS Def. Modified?</th>
            <th>DS Def Verified?</th>
            <th>Designation</th>
            <th>Hazardous</th>
            <th>test column</th>
            <th>Make/Buy</th>
            <th>Packaging</th>
         </tr>
         <tr>
            <td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td>Make</td>
               <td></td>
            </td>
         </tr>
         <tr>
            <td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td>Make</td>
               <td></td>
            </td>
         </tr>
      </table>
   </body>
</html>

This is just as it should be. htm 表中有两行。以下是第二个场景的输出,其中我所做的一切都是从 for-each 选择值中获取 plm:ProductRevision 并将其附加到 的开头value-of 选择语句。两者应该是同义词不是吗?这是完成后的输出。

<html xmlns:plm="http://www.plmxml.org/Schemas/PLMXMLSchema" xmlns:plmxml_cl="http://www.plmxml.org/Schemas/PLMXMLClassificationSchema">
   <body>
      <h2>My CD Collection</h2>
      <table border="1">
         <tr bgcolor="#9acd33">
            <th>Category Type</th>
            <th>DS Def. Modified?</th>
            <th>DS Def Verified?</th>
            <th>Designation</th>
            <th>Hazardous</th>
            <th>test column</th>
            <th>Make/Buy</th>
            <th>Packaging</th>
         </tr>
         <tr>
            <td>
               <td> </td>
               <td> </td>
               <td> </td>
               <td> </td>
               <td> </td>
               <td>Make Make</td>
               <td> </td>
            </td>
         </tr>
      </table>
   </body>
</html>

请帮助我理解为什么。我需要采用第二种方法的原因是我的节点值在 plm:PLMXML 的父节点下不同。换句话说,我在 plm:PLMXML/plm:ProductRevision 和 plm:PLMXML/plmxml_cl:ICO 下有值,所以我想使用

<xsl:for-each select="plm:PLMXML>
<tr>
<td>
    <xsl:value-of
        select="plm:ProductRevision/plm:UserData/plm:UserValue[2]/@value"/>
</td>
<td>
    <xsl:value-of
        select="plmxml_cl:ICO/plmxml_cl:Property[@attributeId='3000011']/plmxml_cl:Value"/>
</td>
</tr>
</for-each>

但是,如上所示,它不会输出所需的代码。任何人可以做任何事情来帮助我理解这一点,我将不胜感激。如果需要任何其他信息,请告知,我会立即提供。谢谢你!

When I use

<xsl:for-each select="plm:PLMXML/plm:ProductRevision>

and the value-of as in

<td><xsl:value-of select="plm:UserData/plm:UserValue[1]/@value"/></td>

follows the web page I am forming with the xsl works perfectly. But when I use

<xsl:for-each select="plm:PLMXML>

and a value-of as in

<td>
    <xsl:value-of 
        select="plm:ProductRevision/plm:UserData/plm:UserValue[1]/@value"/>
</td>

the web page I am creating is no longer formed properly.

In the first case the htm is output like this:

<body>
      <h2>My CD Collection</h2>
      <table border="1">
         <tr bgcolor="#9acd33">
            <th>Category Type</th>
            <th>DS Def. Modified?</th>
            <th>DS Def Verified?</th>
            <th>Designation</th>
            <th>Hazardous</th>
            <th>test column</th>
            <th>Make/Buy</th>
            <th>Packaging</th>
         </tr>
         <tr>
            <td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td>Make</td>
               <td></td>
            </td>
         </tr>
         <tr>
            <td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td>Make</td>
               <td></td>
            </td>
         </tr>
      </table>
   </body>
</html>

This is just as it should be. There will be two rows in an htm table. The following is output from the second scenario where all I have done is taken plm:ProductRevision from the for-each select value and appended it to the beginning of the value-of select statement. The two should be synonymous shouldn't it? Here is the output when that is done.

<html xmlns:plm="http://www.plmxml.org/Schemas/PLMXMLSchema" xmlns:plmxml_cl="http://www.plmxml.org/Schemas/PLMXMLClassificationSchema">
   <body>
      <h2>My CD Collection</h2>
      <table border="1">
         <tr bgcolor="#9acd33">
            <th>Category Type</th>
            <th>DS Def. Modified?</th>
            <th>DS Def Verified?</th>
            <th>Designation</th>
            <th>Hazardous</th>
            <th>test column</th>
            <th>Make/Buy</th>
            <th>Packaging</th>
         </tr>
         <tr>
            <td>
               <td> </td>
               <td> </td>
               <td> </td>
               <td> </td>
               <td> </td>
               <td>Make Make</td>
               <td> </td>
            </td>
         </tr>
      </table>
   </body>
</html>

Please help me understand why. The reason I need to do the second way is I have values from nodes which differ under the parent node of plm:PLMXML. In other words I have values under plm:PLMXML/plm:ProductRevision and under plm:PLMXML/plmxml_cl:ICO so I want to use

<xsl:for-each select="plm:PLMXML>
<tr>
<td>
    <xsl:value-of
        select="plm:ProductRevision/plm:UserData/plm:UserValue[2]/@value"/>
</td>
<td>
    <xsl:value-of
        select="plmxml_cl:ICO/plmxml_cl:Property[@attributeId='3000011']/plmxml_cl:Value"/>
</td>
</tr>
</for-each>

However as shown above it does not output the desired code. Anything anyone can do to help me understand this would be greatly appreciated. If any additional information is required please advise and I will supply it immediately. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

踏雪无痕 2024-11-02 20:38:20

第一个表达式循环遍历每个 plm:PLMXML/plm:ProductRevision,我猜您有两个(或三个)。第二个表达式循环遍历每个 plm:PLMXML,我猜您只有一个(也可能是两个)。

这些不是等价的表达式。它们循环不同的结构。 for-each 的主体将为 select 匹配的集合中的每个节点运行一次。您的 select 表达式不同,因此它们匹配的内容数量可能不同,无论 for-each 正文中包含什么内容。

The first expression loops over each plm:PLMXML/plm:ProductRevision of which I'm guessing you have two (or three). The second expression loops over each plm:PLMXML of which I'm guessing you have only one (or maybe two).

These are not equivalent expressions. They loop over different constructs. The body of the for-each will run once for each node in the set matched by the select. Your select expressions are different, so the number of things they match can be different, regardless of whatever else is included in the body of the for-each.

冰葑 2024-11-02 20:38:20

除了 @lwburk 的正确答案之外,此行为是由于 仅输出字符串而引起的 someExpression 选择的第一个节点的值。

我的猜测是,如果在第二种情况下将

<xsl:value-of          
   select="plm:ProductRevision/plm:UserData/plm:UserValue[1]/@value"/> 

替换为

  <xsl:for-each select=
      "plm:ProductRevision/plm:UserData/plm:UserValue[1]/@value">
      <xsl:value-of select="."/>
  </xsl:for-each>

,那么结果将与第一个转换完全相同

In addition to the correct answer by @lwburk, this behavior is caused by the fact that <xsl:value-of select="someExpression"> only outputs the string value of the first node selected by someExpression.

My guess is that if in the second case you replace:

<xsl:value-of          
   select="plm:ProductRevision/plm:UserData/plm:UserValue[1]/@value"/> 

with

  <xsl:for-each select=
      "plm:ProductRevision/plm:UserData/plm:UserValue[1]/@value">
      <xsl:value-of select="."/>
  </xsl:for-each>

then the result will be exactly the same as with the first transformation.

起风了 2024-11-02 20:38:20

第二种情况的开始:

plm:ProductRevision/plm:UserData

选择所有 plm:ProductRevision 元素下的所有 plm:UserData 元素。

(看起来您的输出中有两个这样的元素。)

然后表达式继续查找所有这些 UserData 元素中的第一个 UserValue。

最后输出所有这些 UserValue 元素的 @value 属性。

这就是为什么您会在单个 td 中看到“Make Make”。

The start of your second case:

plm:ProductRevision/plm:UserData

Is selecting all the plm:UserData elements under all the plm:ProductRevision elements.

(It looks like there are two of these elements from your output.)

The expression then goes on to find the first UserValue in all these UserData elements.

Finally the @value attributes from all these UserValue elements are output.

This is why you see "Make Make" in a single td.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文