Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7060445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:21:14+00:00 2026-05-28T04:21:14+00:00

Joining data from different tables and displaying them in ordered manner using xslt: I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:21:14+00:00Added an answer on May 28, 2026 at 4:21 am

    Edit: Added templates for ProductInformation

    I did have to change the /info/Products/Product/ProductId in your example XML file so they matched the /info/Customers/Customer/CustomerUsesProduct/ProductUsed/ProductNumber.

    Updated input XML:

    <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>PROD01</ProductId>
          <ProductDescription>ProdDesciption1</ProductDescription>
          <ProductCost>100</ProductCost>
          <CustomerWhoUseThisProduct>
            <CustomerNumber>CUSTID01</CustomerNumber>
          </CustomerWhoUseThisProduct>
        </Product> 
    
        <Product>
          <ProductId>PROD02</ProductId>
          <ProductDescription>ProdDesciption2</ProductDescription>
          <ProductCost>555</ProductCost>
          <CustomerWhoUseThisProduct>
            <CustomerNumber>CUSTID02</CustomerNumber>
          </CustomerWhoUseThisProduct>
        </Product> 
    
        <Product>
          <ProductId>PROD03</ProductId>
          <ProductDescription>ProdDesciption3</ProductDescription>
          <ProductCost>777</ProductCost>
          <CustomerWhoUseThisProduct>
            <CustomerNumber>CUSTID03</CustomerNumber>
          </CustomerWhoUseThisProduct>
        </Product>
    
      </Products>
    </info>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="text"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="node()|@*">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:template>
    
      <xsl:template match="Customers">
        <xsl:text>&#xA;&lt;CustomerInfo&gt;&#xA;</xsl:text>
        <xsl:text>CustomerID|CustomerAddress|CustomerName|ProductIDUsed|ProductCost|ProductDescription&#xA;</xsl:text>
        <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="Customer">
        <xsl:variable name="vId" select="CustomerId"/>
        <xsl:variable name="vName" select="CustomerName"/>
        <xsl:variable name="vAddress" select="CustomerAddress"/>
        <xsl:for-each select="CustomerUsesProduct/ProductUsed/ProductNumber">
          <xsl:variable name="vCost" select="/info/Products/Product[ProductId=current()]/ProductCost"/>
          <xsl:variable name="vDesc" select="/info/Products/Product[ProductId=current()]/ProductDescription"/>
          <xsl:value-of select="concat($vId,'|',$vAddress,'|',$vName,'|',.,'|',$vCost,'|',$vDesc,'&#xA;')"/>
        </xsl:for-each>
      </xsl:template>
    
      <xsl:template match="Products">
        <xsl:text>&#xA;&lt;ProductInformation&gt;&#xA;</xsl:text>
        <xsl:text>ProductID|ProductCost|ProductDescription|CustomerID|CustomerAddress|CustomerName&#xA;</xsl:text>
        <xsl:apply-templates/>
      </xsl:template>
    
      <xsl:template match="Product">
        <xsl:variable name="vId" select="ProductId"/>
        <xsl:variable name="vCost" select="ProductCost"/>
        <xsl:variable name="vDesc" select="ProductDescription"/>
        <xsl:for-each select="CustomerWhoUseThisProduct/CustomerNumber">
          <xsl:variable name="vName" select="/info/Customers/Customer[CustomerId=current()]/CustomerName"/>
          <xsl:variable name="vAddress" select="/info/Customers/Customer[CustomerId=current()]/CustomerAddress"/>      
          <xsl:value-of select="concat($vId,'|',$vCost,'|',$vDesc,'|',.,'|',$vAddress,'|',$vName,'&#xA;')"/>
        </xsl:for-each>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Text output:

    <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
    PROD01|100|ProdDesciption1|CUSTID01|Address1|01NAME
    PROD02|555|ProdDesciption2|CUSTID02|Address2|02NAME
    PROD03|777|ProdDesciption3|CUSTID03|Address3|03NAME
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 tables on which i need to select data from using ms-access
I used to select data from mysql database by joining tables using PHP and
I need to get data from 3 different tables. I know the basics about
I am selecting everything from prod_drop and joining 2 other tables for additional data.
I have 2 tables Main and Maintest. I am using nhibernate to pull data
i'm joining tables like: select * from tableA a full join tableB b on
I have a view that is joining two tables and ordering by the first
I have a relatively simple query joining two tables. The Where criteria can be
I am building a data warehouse. I need to get data from different sources
I have two complex rails (AR) queries coming from two different methods that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.