How does preceding-sibling::* work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • senglory
    New Member
    • Sep 2008
    • 11

    How does preceding-sibling::* work?

    My XSL:
    Code:
    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      version="1.0">
      
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
      
      <xsl:template match="w:body">
    
                            <xsl:for-each select="//w:p">
        <xsl:variable name="COUNTER2" select="count(preceding-sibling::*) + 1" />
    <xsl:message><xsl:value-of select="$COUNTER2"/> 
    </xsl:message>
                            </xsl:for-each>
      </xsl:template>
    
    </xsl:stylesheet>

    XML:

    Code:
    <?xml version="1.0"?>
    <w:document  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
        <w:body>
    
            <w:p>
            </w:p>
            
            <w:p>
            </w:p>
            
            <w:p>
            </w:p>
            
            <w:tbl>
                <w:tr w:rsidR="0065563E" w:rsidTr="0065563E">
                    <w:trPr>
                        <w:cnfStyle w:val="100000000000"/>
                    </w:trPr>
                    <w:tc>
                        <w:tcPr>
                            <w:cnfStyle w:val="001000000000"/>
                            <w:tcW w:w="2394" w:type="dxa"/>
                        </w:tcPr>
                        <w:p w:rsidR="0065563E" w:rsidRDefault="003A0CBE" w:rsidP="0015368C"><ADDED>1-2</ADDED>
                            <w:r>
                                <w:t>Header</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="2394" w:type="dxa"/>
                        </w:tcPr>
                        <w:p w:rsidR="0065563E" w:rsidRDefault="0065563E" w:rsidP="0015368C">
                            <w:pPr>
                                <w:cnfStyle w:val="100000000000"/>
                            </w:pPr>
                        </w:p>
                    </w:tc>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="2394" w:type="dxa"/>
                        </w:tcPr>
                        <w:p w:rsidR="0065563E" w:rsidRDefault="0065563E" w:rsidP="0015368C">
                            <w:pPr>
                                <w:cnfStyle w:val="100000000000"/>
                            </w:pPr>
                        </w:p>
                    </w:tc>
                </w:tr>
            </w:tbl>
            
            <w:p>
                
            </w:p>
            
            <w:p>
                <w:r>
                    <w:t>Image inserted</w:t>
                </w:r>
            </w:p>
    
        </w:body>
    </w:document>
    Why do I get this dump:

    1
    2
    3
    2
    2
    2
    5
    6
    I expected it to produce 1,2,3,4... so one so forth. Why is "2" repeated 3 times?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    because at that position there is 3 times only one preceding sibling. (mind, you are in a "subdirecto ry")

    Comment

    • Catz
      New Member
      • Feb 2010
      • 3

      #3
      You can create a simple <xsl:for-each select="//w:p"/> and print the counter in it. You will get 1,2,3,4.. and so on.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Sorry for bumping the thread, but
        use preceding::
        instead of preceding-sibling::

        Comment

        Working...