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 September 10th, 2009, 01:16 PM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Default Insert multiple elements based on values of others

I would like to insert the appropriate amount of doc2c elements in my INPUT XML below. There are an unlimited, variable amount of <doc> elements identified by <docid>. In the DESIRED OUTPUT below, <CID> is contstant and equal to the C/CID element. How can I do this with XLST? The first doc2c will always come after <L_TO_C> and before <morestuff>. Thanks.

INPUT XML:

Code:
<C>
<CID>65938</CID>
</C>
<doc>
<docid>1</ docid >
</doc>
<doc>
< docid >67</ docid >
</doc>
<doc>
< docid >45</ docid >
</doc>


<REF>
			<L_TO_C>
				<L>123</L>
				<CID>987</ CID >
			</ L_TO_C >

<morestuff></morestuff>
</REF>


DESIRED OUTPUT:

Code:
<C>
<CID>65938</CID>
</C>
<doc>
<docid>1</ docid >
</doc>
<doc>
< docid >2</ docid >
</doc>
<doc>
< docid >3</ docid >
</doc>


<REF>
			<L_TO_C>
				<L>123</L>
				<CID>987</ CID >
			</ L_TO_C >
			<doc2c>
				< docid >1</ docid >
				<CID>65938</ CID >
			</ doc2c >
	<doc2c>
				< docid >67</ docid >
				< CID >65938</ CID >
			</ doc2c >
	<doc2c>
				< docid >45</ docid >
				< CID >65938</ CID >
			</ doc2c >


<morestuff></morestuff>
</REF>
 
Old September 11th, 2009, 01:36 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Your input xml did not contain any root element. So I have added a root element to your input xml.
__________________
Rummy

Last edited by mrame; September 11th, 2009 at 08:00 AM..
 
Old September 11th, 2009, 07:07 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Assuming the XML input is
Code:
<root>
<C>
<CID>65938</CID>
</C>
<doc>
<docid>1</docid>
</doc>
<doc>
<docid>67</docid>
</doc>
<doc>
<docid>45</docid>
</doc>


<REF>
            <L_TO_C>
                <L>123</L>
                <CID>987</CID>
            </L_TO_C>

<morestuff></morestuff>
</REF>
</root>
then this stylesheet
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  
  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="doc/docid">
    <xsl:copy>
      <xsl:number count="doc"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="REF">
    <xsl:copy>
      <xsl:apply-templates select="@* | L_TO_C"/>
      <xsl:apply-templates select="/root/doc" mode="m1"/>
      <xsl:apply-templates select="morestuff"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="doc" mode="m1">
    <doc2c>
      <xsl:apply-templates mode="m1"/>
      <xsl:apply-templates select="/root/C/CID"/>
    </doc2c>
  </xsl:template>
  
  <xsl:template match="doc/docid" mode="m1">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
     
</xsl:stylesheet>
outputs the following result:
Code:
<root>
   <C>
      <CID>65938</CID>
   </C>
   <doc>
      <docid>1</docid>
   </doc>
   <doc>
      <docid>2</docid>
   </doc>
   <doc>
      <docid>3</docid>
   </doc>
   <REF>
      <L_TO_C>
         <L>123</L>
         <CID>987</CID>
      </L_TO_C>
      <doc2c>
         <docid>1</docid>
         <CID>65938</CID>
      </doc2c>
      <doc2c>
         <docid>67</docid>
         <CID>65938</CID>
      </doc2c>
      <doc2c>
         <docid>45</docid>
         <CID>65938</CID>
      </doc2c>
      <morestuff/>
   </REF>
</root>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog

Last edited by Martin Honnen; September 11th, 2009 at 08:09 AM.. Reason: correcting stylesheet error
 
Old September 11th, 2009, 08:02 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Assuming as root element, try the below:
__________________
Rummy

Last edited by mrame; September 11th, 2009 at 08:04 AM..
 
Old September 16th, 2009, 05:58 PM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Default Result xml not correct

Thank you for the provided stylesheet. I used it and see that the <doc2c > elements are inserted depending on the number of <doc> elements I have in the input XML. However, strangely, there is other content of the <doc> container showing up in the <doc2c> containers.

I left out some child elements of <doc> that perhaps should have been in my example xml - see EMB below. A more complete example follows. Will this change the stylesheet?

Code:
<root>
<C>
<CID>65938</CID>
</C>
<doc>
<EMB><otherchildren/></EMB>
<docid>1</ docid >
</doc>
<doc>
<EMB><otherchildren/></EMB>
< docid >67</ docid >
</doc>
<doc>
<EMB><otherchildren/></EMB>
< docid >45</ docid >
</doc>


<REF>
			<L_TO_C>
				<L>123</L>
				<CID>65938</ CID >
			</ L_TO_C >

<morestuff></morestuff>
</REF>
</root>
 
Old September 17th, 2009, 07:16 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Please provide a well-formed XML input sample first. White space between < or </ and the element name is not allowed.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 17th, 2009, 08:05 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

And please also show us the output you get and the output you want for the new XML input sample.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 17th, 2009, 10:23 AM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Default Revised input sample and style sheet

I am posting a revised input sample that is more complete, the style sheet I call WROX STYLE.xls and the output. As you can see the result xml is missing all of the other content from the input xml. I need to maintain the content as is, and just add new <doc2c> elements.

Also, I do not want the xmlns:fo="http://www.w3.org/1999/XSL/Format" in the result xml in the <doc2c> elements.

Can you assist?

Input Xml:
Code:
<?xml-stylesheet type="text/xsl" href="WROX STYLE.xsl"?>
<Tremble>
	<Dakota>
		<L>
			<Li>123456</Li>
		</L>
		<C>
			<CID>69538</CID>
		</C>
		<PAR/>
		<PAR/>
		<doc>
			<EMB>
				<pinfo>
					<pid>2</pid>
					<pd>desc</pd>
				</pinfo>
				<enc>
					<pic>gberish2</pic>
				</enc>
			</EMB>
			<docid>1</docid>
			<dt>pho</dt>
		</doc>
		<doc>
			<EMB>
				<pinfo>
					<pid>4</pid>
					<pd>desc</pd>
				</pinfo>
				<enc>
					<pic>gberish4</pic>
				</enc>
			</EMB>
			<docid>67</docid>
			<dt>pho</dt>
		</doc>
		<doc>
			<EMB>
				<pinfo>
					<pid>6</pid>
					<pd>desc</pd>
				</pinfo>
				<enc>
					<pic>gberish6</pic>
				</enc>
			</EMB>
			<docid>45</docid>
			<dt>pho</dt>
		</doc>
		<REF>
			<L_TO_C>
				<L>123</L>
				<CID>69538</CID>
			</L_TO_C>
			<r2p>part1</r2p>
			<r2p>part1</r2p>
		</REF>
		<DID/>
	</Dakota>
	<ver>22</ver>
</Tremble>
stylesheet:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>
  
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="doc/docid">
    <xsl:copy>
      <xsl:number count="doc"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="REF">
    <xsl:copy>
      <xsl:apply-templates select="@* | L_TO_C"/>
      <xsl:apply-templates select="/Tremble/Dakota/doc" mode="m1"/>
      <xsl:apply-templates select="r2p"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="doc" mode="m1">
    <doc2c>
      <xsl:apply-templates mode="m1"/>
      <xsl:apply-templates select="/Tremble/Dakota/C/CID"/>
    </doc2c>
  </xsl:template>
  
  <xsl:template match="doc/docid" mode="m1">
    <xsl:copy>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
Ouput:
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="WROX STYLE.xsl"?>

Code:
<Tremble>
	<Dakota>
		<L>
			<Li/>
		</L>
		<C>
			<CID/>
		</C>
		<PAR/>
		<PAR/>
		<doc>
			<EMB>
				<pinfo>
					<pid/>
					<pd/>
				</pinfo>
				<enc>
					<pic/>
				</enc>
			</EMB>
			<docid>1</docid>
			<dt/>
		</doc>
		<doc>
			<EMB>
				<pinfo>
					<pid/>
					<pd/>
				</pinfo>
				<enc>
					<pic/>
				</enc>
			</EMB>
			<docid>2</docid>
			<dt/>
		</doc>
		<doc>
			<EMB>
				<pinfo>
					<pid/>
					<pd/>
				</pinfo>
				<enc>
					<pic/>
				</enc>
			</EMB>
			<docid>3</docid>
			<dt/>
		</doc>
		<REF>
			<L_TO_C>
				<L/>
				<CID/>
			</L_TO_C>
			<doc2c xmlns:fo="http://www.w3.org/1999/XSL/Format">
				<docid/>
				<CID/>
			</doc2c>
			<doc2c xmlns:fo="http://www.w3.org/1999/XSL/Format">
				<docid/>
				<CID/>
			</doc2c>
			<doc2c xmlns:fo="http://www.w3.org/1999/XSL/Format">
				<docid/>
				<CID/>
			</doc2c>
			<r2p/>
			<r2p/>
		</REF>
		<DID/>
	</Dakota>
	<ver/>
</Tremble>
 
Old September 17th, 2009, 12:08 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can easily get rid of xmlns:fo="http://www.w3.org/1999/XSL/Format" in the result if you don't put that in the stylesheet at all.
As for the other problems, how do you run the transformation exactly (i.e. which XSLT processor do you use, how do you look at the result)?
I am not sure why you have a result with nearly all text missing. That should not happen and does not happen when I run the transformation.
__________________
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:
fixit (September 17th, 2009)
 
Old September 17th, 2009, 12:34 PM
Authorized User
 
Join Date: Sep 2009
Posts: 14
Thanks: 6
Thanked 0 Times in 0 Posts
Default Fixed

I removed the <?xml version="1.0" encoding="UTF-8"?> from the xml input file and the transform seems to work correctly. Thank you for that. Now, I only want to insert the <doc2c> elements if the count of doc2c elements is zero.

Can I incorporate this rule into this stylesheet?





Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple values insert into one field mateenmohd Classic ASP Basics 0 May 29th, 2007 02:11 AM
insert multiple records into a table from values Deepak Chauhan Oracle 3 May 12th, 2006 10:35 PM
insert multiple checkbox values in to database [email protected] Pro JSP 0 March 29th, 2006 08:23 AM
How t o insert multiple values qazi_nomi Access ASP 2 May 3rd, 2005 12:45 AM
convert elements based on place in document jefke XSLT 2 May 17th, 2004 05:32 AM





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