Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 31st, 2010, 12:16 AM
Registered User
 
Join Date: Dec 2009
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My current template:
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:tbl//w:p">
    <xsl:variable name="COUNTER2" select="concat('1', concat('-', count(preceding-sibling::*) + 1))" />
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:variable name="n">
        <xsl:number from="w:tbl" level="any"/>
      </xsl:variable>
      <xsl:if test="$n = 1">
        <ADDED><xsl:value-of select="$COUNTER2"/></ADDED>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="w:p[not(ancestor::w:tbl)]">
    <xsl:variable name="COUNTER2" select="concat('1', concat('-', count(preceding-sibling::*) + 1))" />
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
        <ADDED><xsl:value-of select="$COUNTER2"/></ADDED>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

My input 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>

What I get as an output:


Code:
<?xml version="1.0"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>

        <w:p><ADDED>1-1</ADDED>
        </w:p>
        
        <w:p><ADDED>1-2</ADDED>
        </w:p>
        
        <w:p><ADDED>1-3</ADDED>
        </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><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><ADDED>1-5</ADDED>
            
        </w:p>
        
        <w:p><ADDED>1-6</ADDED>
            <w:r>
                <w:t>Image inserted</w:t>
            </w:r>
        </w:p>

    </w:body>
</w:document>

What I need:
Code:
<?xml version="1.0"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>

        <w:p><ADDED>1-1</ADDED>
        </w:p>
        
        <w:p><ADDED>1-2</ADDED>
        </w:p>
        
        <w:p><ADDED>1-3</ADDED>
        </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>
<!-- DISCREPANCY -->
                    <w:p w:rsidR="0065563E" w:rsidRDefault="003A0CBE" w:rsidP="0015368C"><ADDED>1-4</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><ADDED>1-5</ADDED>
            
        </w:p>
        
        <w:p><ADDED>1-6</ADDED>
            <w:r>
                <w:t>Image inserted</w:t>
            </w:r>
        </w:p>

    </w:body>
</w:document>

How to do it with one template that covers both w:p inside of w:tbl and standalone w:p?
 
Old January 31st, 2010, 08:09 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I don't think a single template helps. What you could do is use two processing steps where the first adds the 'ADDED' elements and the second numbers them:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  xmlns:exsl="http://exslt.org/common"
  exclude-result-prefixes="exsl"
  version="1.0">
  
  <xsl:template match="/">
    <xsl:variable name="temp1">
      <xsl:apply-templates/>
    </xsl:variable>
    <xsl:apply-templates select="exsl:node-set($temp1)" mode="m1"/>
  </xsl:template>

  <xsl:template match="/ | @* | node()" mode="m1">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()" mode="m1"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="ADDED" mode="m1">
    <xsl:copy>
      <xsl:text>1-</xsl:text>
      <xsl:number level="any"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="w:tbl//w:p">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:variable name="n">
        <xsl:number from="w:tbl" level="any"/>
      </xsl:variable>
      <xsl:if test="$n = 1">
        <ADDED></ADDED>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="w:p[not(ancestor::w:tbl)]">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <ADDED/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
With that stylesheet when I transform
Code:
<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">
                        <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>
I get
Code:
<?xml version="1.0" encoding="utf-8"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>

        <w:p><ADDED>1-1</ADDED>
        </w:p>
        
        <w:p><ADDED>1-2</ADDED>
        </w:p>
        
        <w:p><ADDED>1-3</ADDED>
        </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-4</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><ADDED>1-5</ADDED>
            
        </w:p>
        
        <w:p><ADDED>1-6</ADDED>
            <w:r>
                <w:t>Image inserted</w:t>
            </w:r>
        </w:p>

    </w:body>
</w:document>
The only problem with a two step transformation in XSLT 1.0 is that you need to use an extension function to convert a result tree fragment into a node-set. The stylesheet I posted uses the EXSLT node-set function, you will need to check the documentation of your XSLT processor whether it supports that function or maybe a function in a different namespace.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner help: <xsl:apply-template> spits out everything then does the correct match makks XSLT 4 December 21st, 2009 11:05 AM
Question on Apply Template vvenk XSLT 7 July 10th, 2008 12:21 PM
apply-template from included xslt stylesheet chobo XSLT 9 April 16th, 2008 05:25 PM
Difference between call-template ,apply-templates vikkiefd XSLT 4 March 12th, 2008 05:09 AM
Apply Custom template & import data from text file shampabera Excel VBA 2 August 8th, 2005 07:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.