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 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
 
Old May 9th, 2009, 05:22 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
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
 
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>
 
Old May 9th, 2009, 07:23 AM
Friend of Wrox
 
Join Date: Nov 2007
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
 
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.





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





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