2011-03-16 126 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

相關問題