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 29th, 2006, 08:22 AM
Authorized User
 
Join Date: Jun 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT subtree extraction

Hi everybody,

i have troubles extracting subtrees from an xml document.

i'd like to extract everything before a <if> tag and everything after it.

e.g:
Code:
<instruction> ... </instruction>
<instruction> ... </instruction>
<if> .... </if>
<instruction> ... </instruction>
<instruction> ... </instruction>
<instruction> could be any tag. i'd like to extract all the content before and after <if>

<xsl:value-of select="if/preceding-sibling::*" /> doesn't work at all. it extract also the contents of the <if> tag

thanks

 
Old June 29th, 2006, 12:28 PM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

If I start with this:
Code:
<data>
  <element1>1</element1>
  <element2>2</element2>
  <element3>3</element3>
  <if>IF</if>
  <element4>4</element4>
  <element5>5</element5>
  <element6>6</element6>
</data>
and apply this transform:
Code:
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <root>
    <xsl:apply-templates select="data/if/preceding-sibling::*"/>
    <xsl:apply-templates select="data/if/following-sibling::*"/>
  </root>
</xsl:template>

  <xsl:template match="*">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>
I get this:
Code:
<root>
  <element1>1</element1>
  <element2>2</element2>
  <element3>3</element3>
  <element4>4</element4>
  <element5>5</element5>
  <element6>6</element6>
</root>
So you must be doing something else...

--

Joe (Microsoft MVP - XML)
 
Old June 29th, 2006, 06:09 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You haven't specified the problem very precisely, for example, is there always exactly one <if> element?

But: in XSLT 2.0, do

<xsl:copy-of select="* except if"/>

In 1.0, do

<xsl:copy-of select="*[not(name() = 'if')]"/>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 30th, 2006, 02:02 AM
Authorized User
 
Join Date: Jun 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

There are many <if> tags. I have a C program in a XML form. the aim is : 1. extract all <if> tags (the 'control') and put them in a particular place in the dest file. 2. extract all but <if> tags (the 'dataflow') and put them elsewhere. Of course, we must extract them by sub-blocks.

e.g :

Code:
// block 1
inst1;
inst2;
...

// control1
if (c) {
  // block 2
  inst3;
  inst4;

  //control2
  if (c2) {
    // block 3
    ...
  } else {
    // block 4
    ...
  }

  // block 5
  inst....
}

block 6
....
the XML C file is the exact translation of the sourcecode.

i have this template

Code:
  <xsl:template name="dataflow-extract-process-flows">
    <xsl:for-each select="block">

      <xsl:value-of select="if/preceding-sibling::*" />

    </xsl:for-each>
  </xsl:template>
What i missed, is if[position()=1]/preceding-sibling::*, now i can call recursively the template with the rest of the code after the first <if>

Hope this will fix my problem.

Thank you for your help.

 
Old June 30th, 2006, 04:11 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Thanks for the more complete explanation. It's always a good idea to post a complete specimen of the input and desired output.

You have a classic "positional grouping" problem (as it happens, I'm talking at the XIME-P conference in Chicago about positional grouping in XQuery later this morning).

Since you're generating the XML, it would be better to generate the proper hierarchical structure in the first place, rather than trying to infer it later.

In XSLT 2.0, the problem is easily tackled using <xsl:for-each-group group-starting-with="if">.

In 1.0, try a search for "XSLT positional grouping". There are two basic approaches:

(a) sibling recursion: apply-templates to all the <if> children. From that template, apply-templates to following-sibling::*[1][not(self::if)], say in mode="next-sibling". From that template, again apply-templates to following-sibling::*[1][not(self::if)], in mode="next-sibling". The recursion terminates automatically when there's no next sibling or when the next sibling is an <if> element.

(b) reduce it to a value-based grouping problem, which you can tackle using the Muenchian grouping method, in which the grouping key is the generate-id() of "." in the case of an <if> element, or of preceding-sibling::if[1] in the case of any other element.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 30th, 2006, 05:00 AM
Authorized User
 
Join Date: Jun 2006
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

YES ! this seems a very good solution for my problem ! Thanks a lot.
I'm not aware of XSLT 2.0 functions. I practised XSLT 1.0 in the past and i'm having lot of troubles in grouping items. I hope this will work.

Thanks






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Unzip Zip Files using the Extraction Wizard southernsun VB How-To 0 December 6th, 2007 05:03 AM
XML data extraction Neal XSLT 2 March 21st, 2007 06:27 AM
Element extraction Neal XML 3 March 21st, 2007 04:53 AM
regular expression extraction paragraph radhakrishnan1976 General .NET 0 April 7th, 2006 04:35 AM
Ch. 6 - Term Extraction Example ddouglas BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 0 March 20th, 2006 05:11 AM





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