0

我有一个这种格式的 XML:

<Records>
  <Record id="1" type="1">
    <Field type="1">2013-Week 41</Field>
    <Field type="6">219</Field>
    <Field type="7">ABC</Field>
  </Record>
  <Record id="1" type="2">
    <Field type="1">2013-Week 41</Field>
    <Field type="6">220</Field>
    <Field type="7">PQR</Field>
  </Record>
  <Record id="1" type="3">
    <Field type="1">2013-Week 42</Field>
    <Field type="6">221</Field>
    <Field type="7">XYZ</Field>
  </Record>
</Records>

我想根据周合并所有记录元素,例如 2013-Week 41 将包含 219、220 作为子记录,2013-Wee 42 将包含 221 等等。

我想要的输出是这样的:

<Records>
    <Week>
        <Name>2013-Week 41</Name>
        <Week_Task>
            <Value>219</Value>
            <Name>ABC</Name>
        </Week_Task>
        <Week_Task>
            <Value>220</Value>
            <Name>PQR</Name>
        </Week_Task>
    </Week>
    <Week>
        <Name>2013-Week 42</Name>
        <Week_Task>
            <Value>221</Value>
            <Name>XYZ</Name>
        </Week_Task>
    </Week>
</Records>

我如何使用 group by 或 distinct 元素来实现这一点?我需要使用 XSLT 1.0。

4

1 回答 1

1

好吧,如果您想使用 XSLT 1.0,那么您既没有 group-by 也没有 distinct-values,因为它们是 XSLT 2.0 的特性。

使用 XSLT 1.0,您需要使用Muenchian 分组

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:key name="by-week" match="Record" use="Field[@type = 1]"/>

<xsl:template match="Records">
  <xsl:copy>
    <xsl:apply-templates select="Record[generate-id() = generate-id(key('by-week', Field[@type = 1])[1])]"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="Record">
  <Week>
    <Name><xsl:value-of select="Field[@type = 1]"/></Name>
    <xsl:apply-templates select="key('by-week', Field[@type = 1])" mode="task"/>
  </Week>
</xsl:template>

<xsl:template match="Record" mode="task">
  <Week_Task>
    <xsl:apply-templates select="Field[not(@type = 1)]"/>
  </Week_Task>
</xsl:template>

<xsl:template match="Field[@type = 6]">
  <Value><xsl:value-of select="."/></Value>
</xsl:template>

<xsl:template match="Field[@type = 7]">
  <Name><xsl:value-of select="."/></Name>
</xsl:template>

</xsl:stylesheet>
于 2013-10-18T09:58:47.943 回答