Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Grouping nodes into new resultset?

From: Dave@-----------.---------.---
To: NULL
Date: 12/7/2004 12:27:00 PM
Oleg,

Here's what I have so far working in XSelerator utility using both MSXML 3 & 
4 transform engines.  The XML, XSL, and the result is below.  However, in my 
.NET Transform it bombs with "Object reference not set to an instance of an 
object" when I try to transform and load it into an XmlDocument.  Is there 
anything I can do?

XML:
----
<NewDataSet>
  <row>
    <CompanyId>676708</CompanyId>
    <CompanyName>Microsoft Corporation</CompanyName>
    <CountryCd>US</CountryCd>
    <CodeId>20</CodeId>
    <CodeName>AAAA</CodeName>
  </row>
  <row>
    <CompanyId>676708</CompanyId>
    <CompanyName>Microsoft Corporation</CompanyName>
    <CountryCd>US</CountryCd>
    <CodeId>21</CodeId>
    <CodeName>BBBB</CodeName>
  </row>
  <row>
    <CompanyId>676708</CompanyId>
    <CompanyName>Microsoft Corporation</CompanyName>
    <CountryCd>US</CountryCd>
    <CodeId>22</CodeId>
    <CodeName>CCCCCC</CodeName>
  </row>
  <row>
    <CompanyId>676708</CompanyId>
    <CompanyName>Microsoft Corporation</CompanyName>
    <CountryCd>US</CountryCd>
    <CodeId>23</CodeId>
    <CodeName>DDDDD</CodeName>
  </row>
</NewDataSet

XSL:
----
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Define keys used to group elements -->
<xsl:key name="keyCompanyId" match="row" use="CompanyId" />

<xsl:template match="NewDataSet">
<companycodelist>
    <xsl:for-each select="//row[generate-id(.) = 
generate-id(key('keyCompanyId', CompanyId)[1])]">
     <xsl:variable name="lngCompanyId"><xsl:value-of select="CompanyId" 
/></xsl:variable>
     <xsl:variable name="lstRow" select="//row[CompanyId=$lngCompanyId]" />
     <xsl:call-template name="ShowCompanyCodes">
     <xsl:with-param name="lstRow" select="$lstRow" />
     </xsl:call-template>
    </xsl:for-each>
</companycodelist>
</xsl:template>

<xsl:template name="ShowCompanyCodes">
 <xsl:param name="lstRow" />

  <company>
  	 <companyname><xsl:value-of select="$lstRow[1]/CompanyName" 
/></companyname>
	 <codes>
		<xsl:for-each select="$lstRow">
			<xsl:element name="code">
					<xsl:attribute name="id"><xsl:value-of select="CodeId"/></xsl:attribute>
					<xsl:attribute name="name"><xsl:value-of 
select="CodeName"/></xsl:attribute>
			    	<xsl:value-of select="CodeValue" />
			</xsl:element>
		  </xsl:for-each>
	 </codes>
 </company>
</xsl:template>

</xsl:stylesheet>


Result:
-------
<?xml version="1.0" encoding="UTF-16"?>
<companycodelist>
<company>
<companyname>Microsoft Corporation</companyname>
<codes>
	<code id="20" name="AAAA"></code>
	<code id="21" name="BBBB"></code>
	<code id="22" name="CCCCCC"></code>
	<code id="23" name="DDDDD"></code>
</codes>
</company>
</companycodelist>








"Oleg Tkachenko [MVP]" wrote:

> Dave wrote:
> 
> > <?xml version="1.0" ?>
> > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
> > version="2.0">
> 
> You seems to be using XSLT 2.0? Beware neither MSXML nor .NET support 
> XSLT 2.0.
> 
> > <xsl:template match="NewDataSet" mode="grp_company">
> > 	<NewDataSet>
> >      <xsl:for-each select="row[generate-id() = 
> > generate-id(key('company_key', CompanyName)[1])]">
> 
> Well, where is a definition for the company_key ?
> Can you provide more complete XSLT stylesheet?
> -- 
> Oleg Tkachenko [XML MVP]
> http://blog.tkachenko.com
> 


transparent
Print
Mail
Like It
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.

.
.

transparent

transparent