Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 9th, 2009, 04:35 AM
Registered User
 
Join Date: May 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Want to Pull multiple following-sibling data to preceding-sibling element

Hi,
I have one xml were same name of element having multiple following-sibling of preceding element. I am using xslt 1.0
XML

<book>
<sect3 id="a397038pkin" temp="ch0001s0219">
<title>Pharmacokinetics</title>
<para>For further information on pharmacology of antihistamines, <link linkend="a382920pcol">see the Antihistamines General Statement 4:00</link>.</para></sect3>
<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link></sam>

</book>

XSLT
<xsl:template match="//sect3[following-sibling::sam]">
<sect3><xsl:copy-of select="@*" />
<xsl:apply-templates />
<xsl:apply-templates select="following-sibling::sam[1]"/>
</sect3>
</xsl:template>

OUT should render as below
<book>
<sect3 id="a397038pkin" temp="ch0001s0219">
<title>Pharmacokinetics</title>
<para>For further information on pharmacology of antihistamines, <link linkend="a382920pcol">see the Antihistamines General Statement 4:00</link>.</para>

<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link></sam>
</sect3>
</book>

Please Help me
Reply With Quote
  #2 (permalink)  
Old May 9th, 2009, 05:22 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The only thing I can see wrong with your code is the [1] in <xsl:apply-templates select="following-sibling::sam[1]"/>. It looks as if you want to pull all the following siblings into the sect3 element, not only the first.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old May 9th, 2009, 06:03 AM
Registered User
 
Join Date: May 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Want to Pull multiple following-sibling data to preceding-sibling element

Hi
Thanks for reply
If there are multiple <sect3> element then entire <sam> elements appears in every <sect3>
when i removed '[1]'
see below result
<book>
<sect3 id="a397038pkin" temp="ch0001s0219"><title>Pharmacokinetics</title><para>For further information on pharmacology of antihistamines, <link linkend="a382920pcol">see the Antihistamines General Statement 4:00</link>.</para>

<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920di">Drug Interactions in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920lab-test-interf">Lab Test Interferences in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920acute-tox">Acute Toxicity in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920pcol">Pharmacology in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam></sect3>




<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link></sam><sect3 id="a397038pkin" temp="ch0001s0220"><title>DRUG NAME</title><para>For further information on Drug list. </para>

<sam>See <link linkend="a382920di">Drug Interactions in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920lab-test-interf">Lab Test Interferences in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920acute-tox">Acute Toxicity in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920pcol">Pharmacology in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam></sect3>

<sam>See <link linkend="a382920di">Drug Interactions in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920lab-test-interf">Lab Test Interferences in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920acute-tox">Acute Toxicity in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920pcol">Pharmacology in the associated General Statement for more information.</link></sam>

<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam></book>
Reply With Quote
  #4 (permalink)  
Old May 9th, 2009, 07:23 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a sample stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>
  
  <xsl:key name="k1" match="sam" use="generate-id(preceding-sibling::sect3[1])"/>
  
  <xsl:template match="book">
    <xsl:copy>
      <xsl:apply-templates select="sect3"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="sect3">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
      <xsl:apply-templates select="key('k1', generate-id())"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
When applied against the following XML input sample
Code:
<book>
<sect3 id="a397038pkin" temp="ch0001s0219">
<title>Pharmacokinetics</title>
<para>For further information on pharmacology of antihistamines, <link linkend="a382920pcol">see the Antihistamines General Statement 4:00</link>.</para></sect3>
<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link></sam>

<sect3 id="s2" temp="ch0001s0219">
<title>foo</title>
<para>For further information on pharmacology of antihistamines, <link linkend="a382920pcol">see the Antihistamines General Statement 4:00</link>.</para></sect3>
<sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link></sam>
<sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link></sam>

 
</book>
it outputs
Code:
<?xml version="1.0" encoding="utf-8"?>
<book>
   <sect3 id="a397038pkin" temp="ch0001s0219">
      <title>Pharmacokinetics</title>
      <para>For further information on pharmacology of antihistamines, <link linkend="a382920pcol">see the Antihistamines General Statement 4:00</link>.</para>
      <sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link>
      </sam>
      <sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link>
      </sam>
      <sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link>
      </sam>
   </sect3>
   <sect3 id="s2" temp="ch0001s0219">
      <title>foo</title>
      <para>For further information on pharmacology of antihistamines, <link linkend="a382920pcol">see the Antihistamines General Statement 4:00</link>.</para>
      <sam>See <link linkend="a382920pkin">Pharmacokinetics in the associated General Statement for more information.</link>
      </sam>
      <sam>See <link linkend="a382920chem-stab">Chemistry and Stability in the associated General Statement for more information.</link>
      </sam>
      <sam>See <link linkend="a382929uses">Uses in the associated General Statement for more information.</link>
      </sam>
   </sect3>
</book>
If that does not help then you need to provide more details on the possible input and the result you want to create.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
Reply With Quote
  #5 (permalink)  
Old May 9th, 2009, 08:07 AM
Registered User
 
Join Date: May 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Want to Pull multiple following-sibling data to preceding-sibling element

Hi Martin,

It's work without any problem.
Thanku very much.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preceding Sibling mphare XSLT 3 March 4th, 2009 11:04 AM
preceding-sibling::comment() bonekrusher XSLT 11 October 1st, 2008 11:38 AM
Using preceding-sibling mcanne98 Infopath 0 September 11th, 2008 11:09 PM
preceding-sibling descendant xaun XSLT 3 February 28th, 2006 02:48 PM
preceding-sibling jonesyp XSLT 2 November 22nd, 2005 12:29 PM



All times are GMT -4. The time now is 05:27 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.