2012-10-22 93 views
0

對於我原來的XML,我有這樣的:XSLT按唯一元素值分組?

<Data> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type A</Type> 
    <Key>Cases</Key> 
    <Value>3</Value> 
    </Statistic> 
    <Statistic> 
    <Title>PHYSICIAN DETAIL TOTAL</Title> 
    <Type>Type A</Type> 
    <Key>Percentage</Key> 
    <Value>75.0%</Value> 
    </Statistic> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type B</Type> 
    <Key>Cases</Key> 
    <Value>1</Value> 
    </Statistic> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type B</Type> 
    <Key>Percentage</Key> 
    <Value>25.0%</Value> 
    </Statistic> 
</Data> 

基本上,對於每種類型的,只會有一個「案例」和一個「百分比」。 最終的XML看起來像:

<Data> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type A</Type> 
    <count> 
     <Case>1</Case> 
     <Percentage>75%</Percentage> 
    </count> 
    </Statistic> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type B</Type> 
    <count> 
     <Case>1</Case> 
     <Percentage>25%</Percentage> 
    </count> 
    </Statistic> 
</Data> 

什麼是實現這一目標的最佳方式是什麼? XSLT組通過?

+0

xslt 1.0或2.0? – Lukasz

+0

對於' A型',爲什麼它應該選擇'總值',而不是'醫生詳細信息總數'? –

+0

類型A的情況值不應該是3嗎?爲什麼在預期產出的清單中將其作爲1? –

回答

0
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:output indent="yes"/> 

    <xsl:key name="stat-key" match="/Data/Statistic" use="Type"/> 

    <xsl:template match="/"> 
     <Data> 
      <xsl:apply-templates select="/Data/Statistic[generate-id()=generate-id(key('stat-key',Type)[1])]"> 
       <xsl:sort select="Type"/> 
      </xsl:apply-templates> 
     </Data> 
    </xsl:template> 

    <xsl:template match="Statistic"> 
     <xsl:copy> 
      <xsl:copy-of select="Title|Type"/> 
      <count> 
       <Case> 
        <xsl:value-of select="key('stat-key', Type)[Key='Cases']/Value"/> 
       </Case> 
       <Percentage> 
        <xsl:value-of select="key('stat-key', Type)[Key='Percentage']/Value"/> 
       </Percentage> 
      </count> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

它選擇第一StatisticTitle每個Type - 基團。

輸出

<?xml version="1.0" encoding="utf-8"?> 
<Data> 
    <Statistic> 
     <Title>Total Values</Title> 
     <Type>Type A</Type> 
     <count> 
     <Case>3</Case> 
     <Percentage>75.0%</Percentage> 
     </count> 
    </Statistic> 
    <Statistic> 
     <Title>Total Values</Title> 
     <Type>Type B</Type> 
     <count> 
     <Case>1</Case> 
     <Percentage>25.0%</Percentage> 
     </count> 
    </Statistic> 
</Data> 
0

的OP似乎是說,有恰恰是每一個統計案例的孩子,每統計一個準確的百分比孩子。事實如此,分組並不是必需的,解決方案變得微不足道。

**此XSLT 1.0樣式表(也適用於XSLT 2.0)...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" /> 
<xsl:strip-space elements="*" /> 

<xsl:template match="@*|node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Statistic[Key='Percentage']" /> 

<xsl:template match="Statistic"> 
    <xsl:copy> 
    <xsl:apply-templates select="@*|node()[not(self::Key|self::Value)]"/> 
    <count> 
     <Case><xsl:value-of select="Value" /></Case> 
     <Percentage><xsl:value-of select= 
     "../Statistic[Type=current()/Type][Key='Percentage']/Value" /> 
     </Percentage> 
    </count> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 

...當應用於該文檔...

<Data> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type A</Type> 
    <Key>Cases</Key> 
    <Value>3</Value> 
    </Statistic> 
    <Statistic> 
    <Title>PHYSICIAN DETAIL TOTAL</Title> 
    <Type>Type A</Type> 
    <Key>Percentage</Key> 
    <Value>75.0%</Value> 
    </Statistic> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type B</Type> 
    <Key>Cases</Key> 
    <Value>1</Value> 
    </Statistic> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type B</Type> 
    <Key>Percentage</Key> 
    <Value>25.0%</Value> 
    </Statistic> 
</Data> 

...收益...

<Data> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type A</Type> 
    <count> 
     <Case>3</Case> 
     <Percentage>75.0%</Percentage> 
    </count> 
    </Statistic> 
    <Statistic> 
    <Title>Total Values</Title> 
    <Type>Type B</Type> 
    <count> 
     <Case>1</Case> 
     <Percentage>25.0%</Percentage> 
    </count> 
    </Statistic> 
</Data>