2012-01-12 3 views
1

xslt: を使用して、異なるテーブルのデータを結合し、それらを順序付けられた方法で表示します。 1お客様は多くの製品を使用できます。 (1:n関係) 同様の商品は多くのお客様によって消費されます。 (1:n)xsltを使用して異なるノードからデータを結合し、順序付けして表示する

私はコンシューマノード情報を処理している間に、製品情報を取り出して、タブル/ csv形式で表示する必要があります。

私はProductsノード情報を処理している間に、顧客情報を取得して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> 

目的は、1つのテキストファイルで出力することです。出力は次のとおりです。

<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 

期待通りに出力が順序付けられている(つまり列が順序付けられている)と考えている場合は、 私はキーを使用して2つのテーブルに参加しているように見えます。

お客様の処理中。

顧客/顧客/ CustomerUsesProduct/ProductUsed/ProductNumber =製品/製品/のProductId

製品を処理している間。アプリケーションがハングすることができる円形の自己参照がある文句を言わないことを確実にする方法

製品/商品/ CustomerWhoUseThisProduct/CustomerNumberの=顧客/商品/ CustomerIdの

各ノードが他を参照しているので、。

ありがとうございます。

答えて

0

編集:ProductInformation

のための追加されたテンプレートは、私は、彼らが/info/Customers/Customer/CustomerUsesProduct/ProductUsed/ProductNumberにマッチしたように、あなたの例のXMLファイルで/info/Products/Product/ProductIdを変更する必要がありました。

更新入力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 
関連する問題