开发者

Why does the for-each not work consistently?

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!


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜