Altova Mailing List Archives


Re: xslt recursion help...

From: jeremy@-----------.---------.---
To: NULL
Date: 11/7/2004 7:13:00 PM
Thank you Martin for your help so far...it is greatly appreciated.  The 
assumption you made with the sample template that you provided was on target 
with the direction I want to go with the line items.

Now I am going to complicate things...at least for me the that is.....

In the following inbound document, I can also have 1 header, 1 vendor, along 
with many Line Items...but I need to include the header and vendor record 
with the lineitem records using the previous scenario where the max count of 
any invoice can have only 20 lineitems.  If it is more than 20, keep creating 
new invoices duplicating the header and vendor records and remaining line 
items until there
are no lineItems remmaining.  Does this completely change the template?


Also, being the novice that I am at this, is there any good reading on xslt 
that would help me out that you could recommend?

With that being said........

Inbound:

<?xml version="1.0" encoding="UTF-8"?> 
<Invoices> 
<Invoice LineItemCount="25"> 
	<Header>Some header information of inbound invoice 1</Header>
	<Vendor>Some vendor information of inbound invoice 1</Vendor>
	<LineItems></LineItems> 
                <LineItems></LineItems> 
                <LineItems></LineItems> 
                <LineItems></LineItems> 
                <LineItems></LineItems> 
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems> 
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems> 
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems> 
                <LineItems></LineItems>  
                <LineItems></LineItems>  
</Invoice> 
<Invoice LineItemCount="14"> 
	<Header>Some header information of inbound invoice 2</Header>
	<Vendor>Some vendor information of inbound invoice 2</Vendor>
	<LineItems></LineItems> 
	<LineItems></LineItems> 
	<LineItems></LineItems>  
	<LineItems></LineItems> 
	<LineItems></LineItems> 
                <LineItems></LineItems> 
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems> 
                <LineItems></LineItems> 
                <LineItems></LineItems>  
                <LineItems></LineItems>  
                <LineItems></LineItems>  
</Invoice> 
<Invoice LineItemCount="1">
	<Header>Some header information of inbound invoice 3</Header>
	<Vendor>Some vendor information of inbound invoice 3</Vendor>
	<LineItems></LineItems>  
</Invoice> 
</Invoices> 


Output:


<?xml version="1.0" encoding="UTF-8"?> 
<Invoices> 
<Invoice LineItemCount="20"> 
	<Header>Some header information of inbound invoice 1</Header>
	<Vendor>Some vendor information of inbound invoice 1</Vendor>
	<LineItems></LineItems>
	<LineItems></LineItems>
	<LineItems></LineItems>
	<LineItems></LineItems>
	<LineItems></LineItems>
	<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>   
                
</Invoice> 

<Invoice LineItemCount="5">
	<Header>Some header information of inbound invoice 1</Header>
	<Vendor>Some vendor information of inbound invoice 1</Vendor>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
</Invoice>

<Invoice LineItemCount="14"> 
	<Header>Some header information of inbound invoice 2</Header>
	<Vendor>Some vendor information of inbound invoice 2</Vendor>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
		<LineItems></LineItems>
</Invoice> 
<Invoice LineItemCount="1">
	<Header>Some header information of inbound invoice 3</Header>
	<Vendor>Some vendor information of inbound invoice 2</Vendor> 
		<LineItems></LineItems>
</Invoice> 
</Invoices> 



"Martin Honnen" wrote:

> 
> 
> jeremy wrote:
> > I have an inbound invoice document that can have many invoices with 1 to many 
> > line items associated with each invoice.  What I need to do is if the line 
> > items of any one document are greater than 200, then I need to break that 
> > single invoice into 2 separate Invoice elements.  Does anyone have any 
> > suggestion on how to accomplish?
> > 
> > Inbound Doc Structure:
> > 
> > <?xml version="1.0" encoding="UTF-8"?>
> > <Invoices>
> > 	<Invoice LineItemCount="250">
> > 		<LineItems>………………
> 
> That is not well-formed XML, I guess you have
>    <LineItems>------</LineItems>
> but please clarify, in particular we need to know whether the number of 
> <LineItems> element varies and is equal to the value of that 
> LineItemsCount element.
> 
> 
> > Outbound Doc Structure
> > 
> > <?xml version="1.0" encoding="UTF-8"?>
> > <Invoices>
> > 	<Invoice LineItemCount="200">
> > 		<LineItems>………………
> > 	</Invoice>
> > 		
> > 	<Invoice LineItemCount="50">
> > 		<LineItems>………………
> 
> You also need to clarify what happens if the LineItemCount is greater 
> than 400, do you still want two new <Invoice> elements or enough 
> <Invoice> elements to ensure that the LineItemCount is 200 or less?
> 
> The following assumes there as as much <LineItems> elements as 
> LineItemCount indicates, the stylesheet has a parameter maxLineCount 
> that is set to 20 for testing but of course you can set it to 200 or 
> what ever is needed:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>                  version="1.0">
> 
> <xsl:output method="xml" encoding="UTF-8" indent="yes" />
> 
> <xsl:param name="maxLineCount" select="20" />
> 
> <xsl:template match="@* | node()">
>    <xsl:copy>
>      <xsl:apply-templates select="@* |node()" />
>    </xsl:copy>
> </xsl:template>
> 
> <xsl:template match="Invoice">
>    <xsl:for-each select="LineItems[position() mod $maxLineCount = 1]">
>      <xsl:variable name="siblingsToWrap" 
> select="following-sibling::LineItems[position() < $maxLineCount]" />
>      <Invoice LineItemCount="{count($siblingsToWrap) + 1}">
>        <xsl:apply-templates select=". | $siblingsToWrap" />
>      </Invoice>
>    </xsl:for-each>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> With that stylesheet the example input
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <Invoices>
> 	<Invoice LineItemCount="25">
> 		<LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
> 	</Invoice>
> 	<Invoice LineItemCount="14">
> 		<LineItems></LineItems>
> 		<LineItems></LineItems>
> 		<LineItems></LineItems>
> 		<LineItems></LineItems>
> 		<LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
>                  <LineItems></LineItems>
> 	</Invoice>
> 	<Invoice LineItemCount="1">
> 		<LineItems></LineItems>
> 	</Invoice>
> </Invoices>
> 
> is transformed to
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <Invoices>
> 	<Invoice LineItemCount="20">
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>    </Invoice>
>    <Invoice LineItemCount="5">
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>    </Invoice>
> 	<Invoice LineItemCount="14">
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>      <LineItems/>
>    </Invoice>
> 	<Invoice LineItemCount="1">
>      <LineItems/>
>    </Invoice>
> </Invoices>
> 
> 
> -- 
> 
> 	Martin Honnen
> 	http://JavaScript.FAQTs.com/
> 

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.