Joining data from different tables and displaying them in ordered manner using xslt:
I have information about Customers and Products.
1 Customer can use many Products. (1:n relationship)
Similary 1 Product is consumed by many Customers. (1:n)
While I process Consumer node Information, I need to fetch Products information and display it in a tabluar / csv format.
While I process Products node Information, I need to fetch Customer information and display it in a tabluar / csv format.
Input is as below.
<?xml version='1.0' encoding='ISO-8859-1' ?>
<info>
<Customers>
<Customer>
<CustomerName>01NAME</CustomerName>
<CustomerAddress>Address1</CustomerAddress>
<CustomerUsesProduct>
<ProductUsed>
<ProductNumber>PROD01</ProductNumber>
<ProductNumber>PROD02</ProductNumber>
</ProductUsed>
</CustomerUsesProduct>
<CustomerId>CUSTID01</CustomerId>
</Customer>
<Customer>
<CustomerName>02NAME</CustomerName>
<CustomerAddress>Address2</CustomerAddress>
<CustomerUsesProduct>
<ProductUsed>
<ProductNumber>PROD01</ProductNumber>
</ProductUsed>
</CustomerUsesProduct>
<CustomerId>CUSTID02</CustomerId>
</Customer>
<Customer>
<CustomerName>03NAME</CustomerName>
<CustomerAddress>Address3</CustomerAddress>
<CustomerUsesProduct>
<ProductUsed>
<ProductNumber>PROD03</ProductNumber>
</ProductUsed>
</CustomerUsesProduct>
<CustomerId>CUSTID03</CustomerId>
</Customer>
</Customers>
<Products>
<Product>
<ProductId>PROD1</ProductId>
<ProductDescription>ProdDesciption1</ProductDescription>
<ProductCost>100</ProductCost>
<CustomerWhoUseThisProduct>
<CustomerNumber>CUSTID01</CustomerNumber>
</CustomerWhoUseThisProduct>
</Product>
<Product>
<ProductId>PROD2</ProductId>
<ProductDescription>ProdDesciption2</ProductDescription>
<ProductCost>555</ProductCost>
<CustomerWhoUseThisProduct>
<CustomerNumber>CUSTID02</CustomerNumber>
</CustomerWhoUseThisProduct>
</Product>
<Product>
<ProductId>PROD3</ProductId>
<ProductDescription>ProdDesciption3</ProductDescription>
<ProductCost>777</ProductCost>
<CustomerWhoUseThisProduct>
<CustomerNumber>CUSTID03</CustomerNumber>
</CustomerWhoUseThisProduct>
</Product>
</Products>
</info>
The purpose is to output in one single text file.Output expected is as shown below.
<CustomerInfo>
CustomerID|CustomerAddress|CustomerName|ProductIDUsed|ProductCost|ProductDescription
CUSTID01|Address1|01Name|PROD01|100|ProdDesciption1
CUSTID01|Address1|01Name|PROD02|555|ProdDesciption2
CUSTID02|Address2|02NAME|PROD01|100|ProdDesciption1
CUSTID03|Address3|03NAME|PROD03|777|ProdDesciption3
<ProductInformation>
ProductID|ProductCost|ProductDescription|CustomerID|CustomerAddress|CustomerName
PROD1|100|ProdDesciption1|CUSTID01|Address1|01NAME
PROD2|555|ProdDesciption2|CUSTID02|Address2|02NAME
PROD3|777|ProdDesciption3|CUSTID03|Address3|03NAME
If you consider the output is ordered (i.e. columns are ordered) according to expectation.
It appears I am joining two tables using a key and thats precisely what I am trying to do.
While processing Customers.
Customers/Customer/CustomerUsesProduct/ProductUsed/ProductNumber = Products/Product/ProductId
While processing Products.
Products/Product/CustomerWhoUseThisProduct/CustomerNumber = Customers/Customer/CustomerId
Since each node is referencing other, how to ensure that the there wont be a circular self reference whereby the application may hang.
Thanks in advance.
Edit: Added templates for ProductInformation
I did have to change the
/info/Products/Product/ProductIdin your example XML file so they matched the/info/Customers/Customer/CustomerUsesProduct/ProductUsed/ProductNumber.Updated input XML:
XSLT 1.0
Text output: