2012-10-22 95 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>