0

对于我的原始 XML,我有类似的东西:

<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 分组依据?

4

2 回答 2

0

OP 似乎是在说每个统计数据恰好有一个 Case 孩子,每个统计数据恰好有一个 Percentage 孩子。在这种情况下,分组是不必要的,解决方案变得微不足道。

**此 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>
于 2012-10-23T05:41:55.737 回答
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>

它为每个组选择Title第一个。StatisticType

输出

<?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>
于 2012-10-22T23:05:40.193 回答