2012-01-12 53 views
1

連接數據的有序的方式顯示他們使用XSLT有序的方式顯示他們: 我對客戶和產品信息。 1客戶可以使用許多產品。 (1:n關係) 相似1產品被許多客戶消費。 (1:N)加入來自不同節點的數據,並使用來自不同表的XSLT

雖然我處理消費者節點信息,我需要獲取產品信息,並在tabluar/csv格式顯示它。

雖然我處理產品節點的信息,我需要獲取客戶信息,並在tabluar/CSV格式顯示。

輸入如下。

<?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> 

目的是在一個文本文件中輸出。預期輸出如下所示。

<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 

如果您認爲根據預期輸出是有序的(即列有序)。 看來我加入使用鍵兩個表和多數民衆贊成正是我想要做的事。

在處理客戶。

顧客/客戶/ CustomerUsesProduct /產品採用/ ProductNumber =產品/產品/產品編號

雖然加工產品。

產品/產品/ CustomerWhoUseThisProduct/CUSTOMERNUMBER =客戶/客戶/客戶ID

由於每個節點被引用其它,如何確保有不會是一個圓形的自參考由此應用可以掛起。

在此先感謝。

回答

0

編輯:爲ProductInformation

添加模板我也必須改變/info/Products/Product/ProductId在您的示例XML文件,以便它們匹配/info/Customers/Customer/CustomerUsesProduct/ProductUsed/ProductNumber

更新輸入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> 

文本輸出:

<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 
相關問題