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 June 15th, 2009, 12:08 PM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Looking for good example...

I have a problem that I'm certain has been solved and probably has an elegant solution, but after much looking, I can't find it. I'm hoping someone can give me a pointer.

I need to process an xml document with XSLT such that I use one mode for all nodes that precede some identifiable node within the document and an a different mode for all subsequent nodes. IOW, I want to partition the tree into two parts and process each with a different collection of templates.

I have a relatively straightforward solution, but it relies on my providing an explicit value for a position in order to construct the predicate to separate the two halves.

It seems so simple, I'm sure someone has an elegant solution.

Thanks for your help.

--Don
 
Old June 15th, 2009, 12:23 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Can you show us a sample input and your current solution? Then it might become clearer what you are trying to achieve.
Also tell us whether you want to use XSLT 2.0 or 1.0.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old June 15th, 2009, 04:31 PM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Martin,

I'm actually working with XSLT 2.0, so a solution depending on new features is fine.

My current code l is shown below. It does not operate exactly as I would like, though the difference is small. It doesn't use a mode for the first part of the document and I have much more knowledge of the tree elements than I would like in the base template.

The template is actually parsing a modified Word 2007 xml document though I've done a good deal of elimination of elements and changed the namespace prior to invoking this template. This is a stage in which I'm applying some corrections for poor style choices on the part of the document author in preparation for parsing the document itself.

Hope this helps - thanks for suggestions.

--Don

Code:
<xsl:stylesheet version="2.0"
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"
        xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
        xmlns:g="gptg:PMSA:gptg.net">

  <xsl:output method="xml" omit-xml-declaration="no" indent = "yes" encoding = "utf-8"/>
  <xsl:strip-space elements="*"/>
  <xsl:namespace-alias stylesheet-prefix="w" result-prefix="g"/>
  <xsl:namespace-alias stylesheet-prefix="pkg" result-prefix="g"/>


  <!-- Default template - handles first part of document and serves as a default mode -->
  <!-- **the hard coded position is one of the issues - it corresponds to the node that is splitting the document -->
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:apply-templates select="g:document | g:body | g:paraStyle | g:para[position() &lt; 4] |g:text | text()"/>
    </xsl:copy>
  </xsl:template>

  <!-- template to determine a proper mode for the back portion of the document.   -->
  <!-- **the hard coded values correspond to the location of this node which should really be identified as the node which
       satisfies the match condition -->
  <xsl:template match="/g:document/g:body/g:para[substring(normalize-space(g:text[1]),1,7)='Section']">
    <xsl:variable name="protocol" select="normalize-space(substring-after(substring-before(g:text[1],':'),'Section'))"/>
    <xsl:choose>
      <xsl:when test="$protocol='4'">
        <xsl:apply-templates select="/g:document/g:body/*[position() &gt; 2]" mode="Pro4"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="/g:document/g:body/*[position() &gt; 2]" mode="None"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- copy template in case there is no real work to be done to the back portion of the document -->
  <!--  ** this template is called out separately for purposes of later extension  -->
  <xsl:template match=" node() | @*" mode="None">
    <xsl:copy>
      <xsl:apply-templates select="@*" mode="None"/>
      <xsl:apply-templates mode="None"/>
    </xsl:copy>
  </xsl:template>

  <!-- copy template for the modal templates that work on the back portion of the document -->
  <!--   ** there are many templates matching specific conditions in this mode -->
  <xsl:template match="node() | @*" mode="Pro4">
    <xsl:copy>
      <xsl:apply-templates select="@*" mode="Pro4"/>
      <xsl:apply-templates  mode="Pro4"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
 
Old June 16th, 2009, 09:40 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It has not become much clearer to me what you want to achieve.

XPath 2.0 has the operators << and >> which you can use to check whether node1 is before node2 in document order (<<) or the other way round (>>) so maybe you want e.g.
Code:
<xsl:variable name="split-el" select="/root/foo/bar"/>
<xsl:apply-templates select="/g:document/g:body/*[. &gt;&gt; $split-el]" mode="Pro4"/>
to process those nodes coming after $split-el in mode 'Pro4'.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following User Says Thank You to Martin Honnen For This Useful Post:
doxley (June 16th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Good, but not gospel dunxd BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 2 September 6th, 2005 09:14 AM
So Far So Good srichart BOOK: Expert One-on-One Access Application Development 5 July 8th, 2004 10:15 PM
good book Jim Shores BOOK: Expert One-on-One Access Application Development 1 July 8th, 2004 07:26 PM





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