2011-03-16 91 views
1

我正在尝试转换此xml。但是我有格式问题。有人请指导我解决这个问题。在此先感谢XSLT无法对基于值的节点进行分组/排序

<?xml version="1.0" encoding="windows-1252"?> 
<XML> 
    <Attributes> 
     <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> 
     </Attribute> 
     <Attribute> 
      <id>402</id> 
      <Name>Gender</Name> 
      <Type>category</Type> 
      <Value>Men</Value> 
     </Attribute> 
     <Attribute> 
      <id>433</id> 
      <Name>HeelHeight</Name> 
      <Type>category</Type> 
      <Value></Value> 
     </Attribute> 
     <Attribute> 
      <id>41</id> 
      <Name>PlusShip</Name> 
      <Type>common</Type> 
      <Value>False</Value> 
      <Path></Path> 
     </Attribute> 
    </Attributes> 
</XML> 

到以下XML。可能有人请给我如何基于的属性/属性/类型值来改造这个XML的一些技巧

<?xml version="1.0" encoding="utf-8" ?> 
<Data Schema="XML A"> 
    <Attributes type="Common"> 
    <Attr id="" name="Buyer ID" value="Lee" /> 
    <Attr id="" name="Enviornment" value="Development" /> 
    <Attr id="" name="Retail" value="" /> 
    <Attr id="" name="PlusShip" value="False" /> 
</Attributes> 
<Attributes type="Category"> 
    <Attr id="" name="Gender" value="Men" /> 
    <Attr id="" name="HeelHeight" value="" /> 
</Attributes> 

回答

0

以下样式表生成所需的结果:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <Data Schema="XML A"> 
      <xsl:apply-templates/> 
     </Data> 
    </xsl:template> 
    <xsl:template match="Attribute[not(Type=following::Type)]"> 
     <Attributes type="{Type}"> 
      <xsl:for-each select="../Attribute[Type=current()/Type]"> 
       <Attr id="{id}" name="{Name}" value="{Value}"/> 
      </xsl:for-each> 
     </Attributes> 
    </xsl:template> 
    <xsl:template match="Attribute"/> 
</xsl:stylesheet> 

输出在您的原文件上:

<Data Schema="XML A"> 
    <Attributes type="category"> 
     <Attr id="402" name="Gender" value="Men"/> 
     <Attr id="433" name="HeelHeight" value=""/> 
    </Attributes> 
    <Attributes type="common"> 
     <Attr id="5" name="Buyer ID" value="Lee"/> 
     <Attr id="331" name="Enviornment" value="Development"/> 
     <Attr id="79" name="Retail" value=""/> 
     <Attr id="41" name="PlusShip" value="False"/> 
    </Attributes> 
</Data> 

编辑:好,让我们摆脱那个丑陋的for-each

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <Data Schema="XML A"> 
      <xsl:apply-templates/> 
     </Data> 
    </xsl:template> 
    <xsl:template match="Attribute[not(Type=following::Type)]"> 
     <Attributes type="{Type}"> 
      <xsl:apply-templates 
       select="../Attribute[Type=current()/Type]" mode="out"/> 
     </Attributes> 
    </xsl:template> 
    <xsl:template match="Attribute" mode="out"> 
     <Attr id="{id}" name="{Name}" value="{Value}"/> 
    </xsl:template> 
    <xsl:template match="Attribute"/> 
</xsl:stylesheet> 

我感觉好多了。

编辑#2:使用Muenchian方法(有排序):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:key name="type" match="Attribute" use="Type"/> 
    <xsl:template match="/"> 
     <Data Schema="XML A"> 
      <xsl:apply-templates select="XML/Attributes/Attribute"> 
       <xsl:sort select="Type" order="descending"/> 
      </xsl:apply-templates> 
     </Data> 
    </xsl:template> 
    <xsl:template 
      match="Attribute[generate-id()=generate-id(key('type', Type)[1])]"> 
     <Attributes type="{Type}"> 
      <xsl:apply-templates 
        select="../Attribute[Type=current()/Type]" mode="out"/> 
     </Attributes> 
    </xsl:template> 
    <xsl:template match="Attribute" mode="out"> 
     <Attr id="{id}" name="{Name}" value="{Value}"/> 
    </xsl:template> 
    <xsl:template match="Attribute"/> 
</xsl:stylesheet> 

产生下列(有序)输出:

<Data Schema="XML A"> 
    <Attributes type="common"> 
     <Attr id="5" name="Buyer ID" value="Lee"/> 
     <Attr id="331" name="Enviornment" value="Development"/> 
     <Attr id="79" name="Retail" value=""/> 
     <Attr id="41" name="PlusShip" value="False"/> 
    </Attributes> 
    <Attributes type="category"> 
     <Attr id="402" name="Gender" value="Men"/> 
     <Attr id="433" name="HeelHeight" value=""/> 
    </Attributes> 
</Data> 
+0

由于Iwburk。优秀的解决方案.... – JohnXsl 2011-03-18 19:29:15

+0

还有一个问题。我如何排序,我会得到这第一个而不是。我曾尝试使用以下xsl:sort,但它不起作用。 任何想法... – JohnXsl 2011-03-18 21:51:14

+1

@lwburk:的确是的。但是,与老年人的“我没有先前的平等”,而不是与Muenchian方法“我是第一个”? – 2011-03-19 01:58:00

相关问题