TheTimes
TheTimes

Reputation: 77

XSL: position() function and Counting: inaccurate results

So I have been trying to understand why XPath is not returning correct values when I am inputting what seems to be correct (syntax wise).

The goal here is to create a Gs1 label that displays the total quantity of "containers" and which container is the current container. (Container {i} of {total})

In this specific example, the for each loop goes through the ASNShipmentContainers section, and understands that there are 6 ASNShipmentContainer within the ASNShipmentContainers, and therefore prints out 6 different labels.

I figured that the position() function would be the best way to return which iteration my foreach loop is on. I googled up and down, and realized that this would be the easiest and correct way to do this. But when implementing it, it constantly returns a "1" on each label instead of incrementing.

Also, when I try to find the value for the total containers, I use: count(ASNShipmentContainers/ASNShipmentContainer), which is the same as what is declared in the foreach loop and should return my total container value of 6. But this also returns a 1.

Here is the relevant code:

<xsl:variable name="ROOT" select="." />
<!--For each loop, knows there are 6 iterations-->
<xsl:for-each select="ASNShipmentContainers/ASNShipmentContainer">

    <Section Margins="5,5">


          <!--Start creation of table that will display info here-->
          <Table Width="275" Height="30" Margins="161,0,0,0">
            <Row Height="100%">
                <Cell Borders="Black" Width=".125"/>
                    <Cell Width="274.375">
                        <Text>Count:<xsl:value-of select="position()" /> of <xsl:value-of select="count(ASNShipmentContainers/ASNShipmentContainer))" /></Text>
                    </Cell>
                <Cell Borders="Black" Width=".5"/>
              </Row>
            </Table>


</Section>

</xsl:for-each>

So why is the value of position and total container quantity both "1" when I do this. It is my understanding that position should work, even though it is being called within the for-each loop.

Also, if I asked for a count of the Containers, shouldn't I receive the total quantity? Why would it give me a 1.

The only thing I can imagine is that somehow my scope is off for my loop/select statements. Would this require another use of ?

Edit: Thank you for your quick responses, but the Issue was actually unrelatesd to the XSL. After finally working with someone who had used this code before, he explained to me that the issue was coming from our C# code, not the XSL itself. The C# would erase all the elements it was not currently working on, so therefore every time I tried to count all the elements, It would give me a one because technically, there was only one.

Thank you.

Upvotes: 0

Views: 184

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117165

When you are in the context of:

<xsl:for-each select="ASNShipmentContainers/ASNShipmentContainer">

the expression:

count(ASNShipmentContainers/ASNShipmentContainer)

tries to count the ASNShipmentContainer elements that are children of ASNShipmentContainers that are children of the current node. I don't think that's what you want.

To count the ASNShipmentContainer elements at the current level, try:

count(../ASNShipmentContainer)

or - preferably - define a variable with the count before calling for-each, in order to avoid repeated counts of the same thing.

Not sure what's your issue with position() - you'll have to post a fuller example so that we can reproduce the problem.

Upvotes: 2

Related Questions