Редактировать: добавлены шаблоны для 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>
<CustomerInfo>
</xsl:text>
<xsl:text>CustomerID|CustomerAddress|CustomerName|ProductIDUsed|ProductCost|ProductDescription
</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,'
')"/>
</xsl:for-each>
</xsl:template>
<xsl:template match="Products">
<xsl:text>
<ProductInformation>
</xsl:text>
<xsl:text>ProductID|ProductCost|ProductDescription|CustomerID|CustomerAddress|CustomerName
</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,'
')"/>
</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