XSLT grouping recursive problem
guys, recursion with grouping is not working. I am using XSLT 1.0. I need some help to figure this out. Thanks in Advance.
Background: I have 3 different Types (common, category, & complex) in the xml. My goal is to.
1 - Group xml nodes based on Types. 2 - Create a sub-group for complex under Type=common. 3 - For Type=complex create a number of collections depending upon the source xml. In each collection it should list only 4 elements where Name='A' or 'B' or 'C' or 'D'. This is where I'm having the problem. Grouping and sub-grouping is working fine. However, when I try to create a collection using recursion then it is not giving me the intending output. For reference please see the expected out xml sample.
Source XML:
<?xml version="1.0" encoding="Windows-1252"?>
<XML>
<Attributes>
<Attribute>
<Name>Buyer ID</Name>
<Type>common</Type>
<Value>Lee</Value>
</Attribute>
<Attribute>
<Name>Enviornment</Name>
<Type>common</Type>
<Value>Dev</Value>
</Attribute>
<Attribute>
<Name>Retail</Name>
<Type>common</Type>
<Value></Value>
</Attribute>
<Attribute>
<Name>Gender</Name>
<Type>category</Type>
<Value>M</Value>
</Attribute>
<Attribute>
<Name>Collection</Name>
<Type>Complex</Type>
<Value>ing</Value>
<Path />
</Attribute>
<Attribute>
<Name>A</Name>
<Type>Complex</Type>
<Value>Testing</Value>
<Path />
</Attribute>
<Attribute>
<Name>B</Name>
<Type>Complex</Type>
<Value>Yellow</Value>
<Path />
</Attribute>
<Attribute>
<Name>C</Name>
<Type>Complex</Type>
<Value>10</Value>
<Path />
</Attribute>
<Attribute>
<Name>D</Name>
<Type>Complex</Ty开发者_运维技巧pe>
<Value>MA</Value>
<Path />
</Attribute>
<Attribute>
<Name>A</Name>
<Type>Complex</Type>
<Value>24a</Value>
<Path />
</Attribute>
<Attribute>
<Name>B</Name>
<Type>Complex</Type>
<Value>Green</Value>
<Path />
</Attribute>
<Attribute>
<Name>C</Name>
<Type>Complex</Type>
<Value>22</Value>
<Path />
</Attribute>
<Attribute>
<Name>D</Name>
<Type>Complex</Type>
<Value>AM</Value>
<Path />
</Attribute>
</Attributes>
</XML>
Expected Output:
<?xml version="1.0" encoding="utf-8"?>
<Data Schema="XML A">
<Items>
<Item>
<Attributes type="common">
<Attr name="Buyer ID" value="Lee" />
<Attr name="Enviornment" value="Dev" />
<Attr name="Retail" value="" />
<Collection name="Collection" >
<Complex>
<Attr name="A" value="Testing" />
<Attr name="B" value="Yellow" />
<Attr name="C" value="10" />
<Attr name="D" value="MA" />
</Complex>
<Complex>
<Attr name="A" value="24a" />
<Attr name="B" value="Green" />
<Attr name="C" value="22" />
<Attr name="D" value="AM" />
</Complex>
</Collection>
</Attributes>
<Attributes type="category">
<Attr name="Gender" value="M" />
</Attributes>
<errorCodes>
<errorCode>value for Retail is missing.</errorCode>
</errorCodes>
</Item>
</Items>
</Data>
Here is the XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="type" match="Attribute" use="Type"/>
<xsl:variable name="group" select="4"/>
<xsl:template match="/">
<Data Schema="XML A">
<Items>
<Item>
<xsl:apply-templates select="XML/Attributes/Attribute[generate-id() = generate-id(key('type', Type)[1])]">
<xsl:sort select="Type" order="descending"/>
</xsl:apply-templates>
<errorCodes>
<xsl:apply-templates select="XML/Attributes/Attribute" mode="errors"/>
</errorCodes>
</Item>
</Items>
</Data>
</xsl:template>
<xsl:template match="Attribute">
<xsl:variable name="compType" select="count(/XML/Attributes/Attribute[Type='Complex' and Name!='Collection'])"/>
<xsl:if test="Type!='Complex'">
<Attributes type="{Type}">
<xsl:apply-templates select="key('type',Type)" mode="out"/>
<xsl:if test="Type='common'">
<Collection>
<xsl:for-each select="/XML/Attributes/Attribute[Type='Complex']">
<xsl:choose>
<xsl:when test="(Name='A' or Name='B' or Name='C' or Name='D')">
<xsl:if test="(($compType > 0) and (Name!='Collection'))">
<xsl:apply-templates select="key('type','Complex')" mode="out"/>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<Complex>
<Attr id="" name="A" value="Default" />
<Attr id="" name="B" value="Default" />
<Attr id="" name="C" value="Default" />
<Attr id="" name="D" value="" />
</Complex>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</Collection>
</xsl:if>
</Attributes>
</xsl:if>
</xsl:template>
<xsl:template match="Attribute" mode="out">
<Collection>
<Attr name="{Name}" value="{Value}"/>
</Collection>
</xsl:template>
<xsl:template match="Attribute[Type='Complex']" mode="out">
<xsl:apply-templates select="XML/Attributes/Attribute[not(Name='Collection')]
[position() mod $group = 1]" mode="group"/>
</xsl:template>
<xsl:template match="Name" mode="group">
<xsl:if test="Name!='Collection'">
<Attr name="{Name}" value="{Value}"/>
</xsl:if>
</xsl:template>
<xsl:template match="Attribute">
<Complex>
<xsl:apply-templates
select=".|following-sibling::Attribute[position() < $group]" mode="inner" />
</Complex>
</xsl:template>
<xsl:template match="Attribute" mode="errors">
<xsl:if test="(Name='Retail' or Name='Product Description') and Value=''">
<errorCode>value for <xsl:value-of select="Name"/> is missing.</errorCode>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I am not sure I understand all your requirements but the following sample
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="http://example.com/data"
exclude-result-prefixes="data"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="att-by-type" match="Attributes/Attribute" use="Type"/>
<xsl:variable name="complex" select="key('att-by-type', 'Complex')"/>
<xsl:template match="XML">
<Data Schema="XML A">
<Items>
<Item>
<xsl:apply-templates select="Attributes/Attribute[Type = 'common' or Type = 'category'][generate-id() = generate-id(key('att-by-type', Type)[1])]" mode="group"/>
</Item>
</Items>
</Data>
</xsl:template>
<xsl:template match="Attribute" mode="group">
<Attributes type="{Type}">
<xsl:apply-templates select="key('att-by-type', Type)"/>
<xsl:if test="Type = 'common'">
<Collection name="Collection">
<xsl:apply-templates select="$complex[Name = 'A']" mode="comp-group"/>
</Collection>
</xsl:if>
</Attributes>
</xsl:template>
<xsl:template match="Attribute" mode="comp-group">
<Complex>
<xsl:variable name="pos" select="position()"/>
<xsl:apply-templates select="$complex[Name = 'A'][position() = $pos] |
$complex[Name = 'B'][position() = $pos] |
$complex[Name = 'C'][position() = $pos] |
$complex[Name = 'D'][position() = $pos]"/>
</Complex>
</xsl:template>
<xsl:template match="Attribute">
<Attr name="{Name}" value="{Value}"/>
</xsl:template>
</xsl:stylesheet>
produces the output you posted (with the exception of the errorCodes, I left that out as it seemed unrelated to the other problem).
精彩评论