可能重复:
XSLT 无法根据属性值对节点进行分组
我有一个独特的情况。我正在尝试使用 XSLT 将一个 XML 转换为另一个。这是我的源 XML
<
<?xml version="1.0" encoding="windows-1252"?><XML>
<Attributes>
<Attribute><id>FW</id><Name>FamilyName</Name><Type>common</Type><Value>Footwear</Value></Attribute>
<Attribute><id>83</id><Name>DepartmentName</Name><Type>common</Type><Value>Footwear</Value></Attribute>
<Attribute><id>560</id><Name>GroupName</Name><Type>common</Type><Value>Men's Boots</Value></Attribute>
<Attribute><id>5</id><Name>Buyer ID</Name><Type>common</Type><Value>Lee</Value></Attribute>
<Attribute><id>331</id><Name>Enviornment</Name><Type>common</Type><Value>Development</Value></Attribute>
<Attribute><id>79</id><Name>Retail</Name><Type>common</Type><Value></Value><Path>Data Attributes//Common Attributes//Vendor</Path></Attribute>
<Attribute><id>1</id><Name>Media</Name><Type>common</Type><Value></Value><Path></Path></Attribute>
<Attribute><id>402</id><Name>Gender</Name><Type>category</Type><Value>Men</Value><Path>Data Attributes//Category Specific//Toys/Infants//Gender</Path></Attribute>
<Attribute><id>433</id><Name>HeelHeight</Name><Type>category</Type><Value></Value><Path>Data Attributes//Category Specific//Apparel//HeelHeight</Path></Attribute>
<Attribute><id>909</id><Name>ShoeStyle</Name><Type>category</Type><Value>Boot</Value><Path>Data Attributes//Category Specific//Apparel//ShoeStyle</Path></Attribute>
<Attribute><id>942</id><Name>Special Shoe Features</Name><Type>category</Type><Value>Waterproof</Value><Path>Data Attributes//Category Specific//Footwear//Special Shoe Features</Path></Attribute>
<Attribute><id>1114</id><Name>Width</Name><Type>category</Type><Value>Medium</Value><Path>Data Attributes//Category Specific//Footwear//Width</Path></Attribute>
<Attribute><id>41</id><Name>PlusShip</Name><Type>common</Type><Value>False</Value><Path></Path></Attribute>
<Attribute><id>42</id><Name>IncShip</Name><Type>common</Type><Value>False</Value><Path></Path></Attribute>
<Attribute><id>43</id><Name>Trademark/Registered Name</Name><Type>common</Type><Value>False</Value><Path>Data Attributes//Category Specific//Toys / Infants//Trademark/Registered Name</Path></Attribute>
</Attributes>
</XML>
而且我需要 XSL 将其转换为以下 xml
<?xml version="1.0" encoding="utf-8" ?>
<Data Schema="XML A">
<Organization id="4" name="ABC">
<Catalog id="15" name="Product">
<Items>
<Item id="" shortname="4442 BLK" longname="4442 BLK" sku="Footwear" action="ADD" categorypath="Footwear/Footwear/Men's Boots" type="Product">
<Attributes type="Common">
<Attr id="" name="Buyer ID" path="" action="ADD" value="Lee" uom="" Locale="en_WW" />
<Attr id="" name="Enviornment" path="" action="ADD" value="Development" uom="" Locale="en_WW" />
<Attr id="" name="Retail" path="Data Attributes//Common Attributes//Vendor//VentureRetail//Retail" action="ADD" value="" uom="" Locale="en_WW" />
<Attr id="" name="Media" path="" action="ADD" value="" uom="" Locale="en_WW" />
<Attr id="" name="Vendor Model" path="Data Attributes//Common Attributes//SKU Details//VendorModel And GTINs//Vendor Model" action="ADD" value="4442 BLK" uom="" Locale="en_WW" />
<Attr id="" name="PlusShip" path="" action="ADD" value="False" uom="" Locale="en_WW" />
<Attr id="" name="IncShip" path="" action="ADD" value="False" uom="" Locale="en_WW" />
<Attr id="" name="Trademark/Registered Name" path="Data Attributes//Category Specific//Toys / Infants//Trademark/Registered Name" action="ADD" value="False" uom="" Locale="en_WW" />
</Attributes>
<Attributes type="Category">
<Attr id="" name="Gender" path="Data Attributes//Category Specific//Toys/Infants//Gender" action="ADD" value="Men" uom="" Locale="en_WW" />
<Attr id="" name="HeelHeight" path="Data Attributes//Category Specific//Apparel//HeelHeight" action="ADD" value="" uom="" Locale="en_WW" />
<Attr id="" name="ShoeStyle" path="Data Attributes//Category Specific//Apparel//ShoeStyle" action="ADD" value="Boot" uom="" Locale="en_WW" />
<Attr id="" name="Special Shoe Features" path="Data Attributes//Category Specific//Footwear//Special Shoe Features" action="ADD" value="Waterproof" uom="" Locale="en_WW" />
<Attr id="" name="Width" path="Data Attributes//Category Specific//Footwear//Width" action="ADD" value="Medium" uom="" Locale="en_WW" />
</Attributes>
<errorCodes>
<errorCode>"value for Retail is missing."</errorCode>
</errorCodes>
</Item>
</Items>
</Catalog>
</Organization>
</Data>
>
如您所见,它将所有“公共”属性节点放在一个基于类型的属性节点中。它还从 Attributes/Attribute/Value 中提取 Items/item/@shortname,其中 Attributes/Attribute/Name = 'Vendor Model'。相同的值用于 longname 和 sku。它从 Items/Item/@value 中提取 Items/Item/@categorypath,其中 Name=FamilyName & Items/Item/@Vvalue,其中 Name= DepartmentName & Items/Item/@Value,其中 Name = GroupName。
我能够将其部分转换为这种格式。但无法聚合 Attr 节点并根据 Attributes/@type 对其进行排序。
<?xml version="1.0" encoding="utf-8" ?>
- <Data Schema="XML A">
- <Organization id="4" name="ABC">
- <Catalog id="15" name="Product Staging">
- <Items>
- <Item id="" shortname="4442 BLK" longname="4442 BLK" sku="Footwear" action="ADD" categorypath="Footwear/Footwear/Men's Boots" type="Product">
- <Attributes type="Common">
<Attr id="" name="Buyer ID" path="" action="ADD" value="Lee" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="Enviornment" path="" action="ADD" value="Development" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="Retail" path="Data Attributes//Common Attributes//Vendor//VentureRetail//Retail" action="ADD" value="" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="Media" path="" action="ADD" value="" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="Vendor Model" path="Data Attributes//Common Attributes//SKU Details//VendorModel And GTINs//Vendor Model" action="ADD" value="4442 BLK" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Category">
<Attr id="" name="Gender" path="Data Attributes//Category Specific//Toys/Infants//Gender" action="ADD" value="Men" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Category">
<Attr id="" name="HeelHeight" path="Data Attributes//Category Specific//Apparel//HeelHeight" action="ADD" value="" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Category">
<Attr id="" name="ShoeStyle" path="Data Attributes//Category Specific//Apparel//ShoeStyle" action="ADD" value="Boot" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Category">
<Attr id="" name="Special Shoe Features" path="Data Attributes//Category Specific//Footwear//Special Shoe Features" action="ADD" value="Waterproof" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Category">
<Attr id="" name="Width" path="Data Attributes//Category Specific//Footwear//Width" action="ADD" value="Medium" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="PlusShip" path="" action="ADD" value="False" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="IncShip" path="" action="ADD" value="False" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="Trademark/Registered Name" path="Data Attributes//Category Specific//Toys / Infants//Trademark/Registered Name" action="ADD" value="False" uom="" Locale="en_WW" />
</Attributes>
- <Attributes type="Common">
<Attr id="" name="Trademark/Registered Name" path="Data Attributes//Category Specific//Toys / Infants//Trademark/Registered Name" action="ADD" value="True" uom="" Locale="en_WW" />
</Attributes>
- <errorCodes>
<errorCode>"value for Retail is missing."</errorCode>
</errorCodes>
</Item>
</Items>
</Catalog>
</Organization>
</Data>
提前致谢