<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" />

   <xsl:key name="Qualifier" match="LIST/LIST_ST/REF" use="concat(count(../following-sibling::LIST_ST), Qualifier)" />
   <xsl:key name="LI-Qualifier" match="LIST/LIST_ST/LINEITEM/REF" use="concat(count(../../following-sibling::LIST_ST), count(../following-sibling::LINEITEM), Qualifier)" />

   <xsl:template match="/">
      <HTML>
         <HEAD>
            <TITLE>850</TITLE>
         </HEAD>

         <xsl:for-each select="LIST/LIST_ST">
            <BODY>
               <H3>Purchase Order Details</H3>

               <H2><xsl:value-of select="concat('Purchase Order ', position(), '-', BEG/PONumber)" /></H2>

               <table border="2" cellpadding="3" rules="all" bgcolor="#D8C454">
                  <TH>Qualifier</TH>
                  <TH>Code</TH>
                  <TH>Text</TH>

                  <xsl:for-each select="REF[generate-id(.) = generate-id( key('Qualifier', concat(count(../following-sibling::LIST_ST), Qualifier))[1]) ]">
                     <TR>
                        <TD><xsl:value-of select="Qualifier" /></TD>
                        <TD><xsl:value-of select="Code" /></TD>

                        <TD>
                           <xsl:variable name="group" select="key('Qualifier', concat(count(../following-sibling::LIST_ST), Qualifier))" />
                           <xsl:variable name="v1">
                              <xsl:for-each select="$group/Text">
                                 <xsl:value-of select="." />
                              </xsl:for-each>
                           </xsl:variable>
                           <xsl:value-of select="$v1" />
                        </TD>
                     </TR>
                  </xsl:for-each>
               </table>

			      
	               <xsl:for-each select="LINEITEM">
	                  <table border="2" cellpadding="3" rules="all" bgcolor="#D8C454">
						<TH>Item number</TH>
						<TH>Qualifier</TH>
						<TH>Code</TH>
						<TH>LINEITEM TEXT</TH>
               
					  <xsl:variable name="ItemNumber1" select="ITEMNUMBER" />
	                     <xsl:for-each select="REF[generate-id(.) = generate-id( key('LI-Qualifier', 
						 	concat(count(../../following-sibling::LIST_ST), count(../following-sibling::LINEITEM), 
							Qualifier))[1]) ]">
	                        <TR>
	                           <TD><xsl:value-of select="$ItemNumber1" /></TD>
	                           <TD><xsl:value-of select="Qualifier" /></TD>
	                           <TD><xsl:value-of select="Code" /></TD>
	                           <TD>
	                              <xsl:variable name="group" select="key('LI-Qualifier', 
								  	concat(count(../../following-sibling::LIST_ST), count(../following-sibling::LINEITEM), 
									Qualifier))" />
	                              <xsl:variable name="v1">
	                                 <xsl:for-each select="$group/Text">
	                                    <xsl:value-of select="." />
	                                 </xsl:for-each>
	                              </xsl:variable>
	                              <xsl:value-of select="$v1" />
	                           </TD>
	                        </TR>
	                     </xsl:for-each>
	               </table>
				 </xsl:for-each>
            </BODY>
         </xsl:for-each>
      </HTML>
   </xsl:template>
</xsl:stylesheet>

<!-- Stylus Studio meta-information - (c) 2004-2009. Progress Software Corporation. All rights reserved.

<metaInformation>
	<scenarios>
		<scenario default="no" name="Scenario1" userelativepaths="yes" externalpreview="no" url="po.xml" htmlbaseurl="" outputurl="" processortype="saxon8" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline=""
		          additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator="">
			<advancedProp name="sInitialMode" value=""/>
			<advancedProp name="bXsltOneIsOkay" value="true"/>
			<advancedProp name="bSchemaAware" value="true"/>
			<advancedProp name="bXml11" value="false"/>
			<advancedProp name="iValidation" value="0"/>
			<advancedProp name="bExtensions" value="true"/>
			<advancedProp name="iWhitespace" value="0"/>
			<advancedProp name="sInitialTemplate" value=""/>
			<advancedProp name="bTinyTree" value="true"/>
			<advancedProp name="bWarnings" value="true"/>
			<advancedProp name="bUseDTD" value="false"/>
			<advancedProp name="iErrorHandling" value="fatal"/>
		</scenario>
		<scenario default="yes" name="po2.xml" userelativepaths="yes" externalpreview="no" url="po2.xml" htmlbaseurl="" outputurl="" processortype="saxon8" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline=""
		          additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator="">
			<advancedProp name="sInitialMode" value=""/>
			<advancedProp name="bXsltOneIsOkay" value="true"/>
			<advancedProp name="bSchemaAware" value="true"/>
			<advancedProp name="bXml11" value="false"/>
			<advancedProp name="iValidation" value="0"/>
			<advancedProp name="bExtensions" value="true"/>
			<advancedProp name="iWhitespace" value="0"/>
			<advancedProp name="sInitialTemplate" value=""/>
			<advancedProp name="bTinyTree" value="true"/>
			<advancedProp name="bWarnings" value="true"/>
			<advancedProp name="bUseDTD" value="false"/>
			<advancedProp name="iErrorHandling" value="fatal"/>
		</scenario>
	</scenarios>
	<MapperMetaTag>
		<MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/>
		<MapperBlockPosition></MapperBlockPosition>
		<TemplateContext></TemplateContext>
		<MapperFilter side="source"></MapperFilter>
	</MapperMetaTag>
</metaInformation>
-->