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 July 6th, 2012, 11:29 AM
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 12
Thanked 0 Times in 0 Posts
Exclamation Can I query an XML document as it's created

Hello.

When creating an XML during a transformation is it possible to query (XPath) the document as it's being built?

For example I have a template that can create an unknown number of elements with an unknown number of attributes, so is it possible to find out how may elements were created after the template has finished?
Code:
<xsl:template name="root">
    <xsl:call-template name="create-unknown-number-of-elements-template" />
    <xsl:call-template name="count-number-of-elements-created-above" />
</xsl:template>
Or am I wasting my time?

--
Bill
 
Old July 6th, 2012, 11:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Typically you'll need to write this as a multi-phase transformation in which the second phase processes the output of the first. Whether the two phases run in series or in parallel is of course entirely up to the optimizer.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
BillMoo (July 6th, 2012)
 
Old July 6th, 2012, 11:59 AM
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by mhkay View Post
Typically you'll need to write this as a multi-phase transformation in which the second phase processes the output of the first. Whether the two phases run in series or in parallel is of course entirely up to the optimizer.
Hi Michael,

Once again "Thank you", I normally wouldn't do this but are you able to provide me with an example of this please?

--
Bill
 
Old July 6th, 2012, 12:06 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Assuming XSLT 2.0 you can do
Code:
<xsl:variable name="temp1">
  <xsl:apply-templates select="node()" mode="m1"/>
</xsl:variable>

<xsl:template match="/">
 <xsl:value-of select="count($temp1//*)"/>
</xsl:template>

<xsl:template match="foo" mode="m1">
  ...
</xsl:template>
I used a mode instead of named templates instead of named templates as your first post does but of course if you want you can populate a variable with a named template.
__________________
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:
BillMoo (July 7th, 2012)
 
Old July 7th, 2012, 12:30 PM
Registered User
 
Join Date: Jun 2012
Posts: 20
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Thank you Martin. But for the life of me I couldn't get it to work so I ended up using nested templates.

--
Bill





Similar Threads
Thread Thread Starter Forum Replies Last Post
chp33 LINQ query Xml Document duce1nik BOOK: Professional C# 4.0 and .NET 4 1 April 15th, 2011 10:43 AM
Split xml file with result document and javax.xml.transform.Transformer. nisargmca XSLT 3 January 12th, 2010 06:26 AM
Retrieving XML created by a form sloanthrasher XML 6 January 16th, 2006 12:25 PM
Using SQL query for Excell document abinashpatra Classic ASP Databases 1 April 7th, 2005 09:47 AM
xml document androger XML 2 November 19th, 2003 05:52 PM





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