繁体   English   中英

XML的XSLT 2.0分组(用于WordML)

[英]XSLT 2.0 Grouping (for WordML) from XML

我有一个时间表列表,可以列出通过WordML在MS Word中通过MSML输出(使用XSLT 2.0),并且认为我需要做一些我不确定如何完成的分组(如果可能的话)。

上层人士正在寻找的是按每个日期(按日期排序),所有信息都被汇总到每个计时员的一个区域中。 (日期,计时员姓名缩写,该日期的工作时间总和,该计时员该日期所有叙事数据的列表)。

这是我当前拥有的内容,仅是每个日期的每个计时员/考勤卡,没有分组。

INPUT:

<xml-sample>
<timecard index="10">
    <timekeeper>
        <initials>NC1</initials>
    </timekeeper>
    <date type="timecard">2015-09-01</date>
    <card-values>
        <card-value type="billed">
            <rate>325.00</rate>
            <hours>0.20</hours>
            <amount>65.0000</amount>
        </card-value>
    </card-values>
    <narrative>
        <line id="1">Narrative for 9/1/2015. With some more detail</line>
        <line id="2">on the 2nd line ID.</line>
    </narrative>
</timecard>
<timecard index="11">
    <timekeeper>
        <initials>NC1</initials>
    </timekeeper>
    <date type="timecard">2015-09-01</date>
    <card-values>
        <card-value type="billed">
            <rate>325.00</rate>
            <hours>0.40</hours>
            <amount>130.0000</amount>
        </card-value>
    </card-values>
    <narrative>
        <line id="1">Another narrative for 9/1/2015. With some more</line>
        <line id="2">detail on the 2nd line ID.</line>
    </narrative>
</timecard>
<timecard index="12">
    <timekeeper>
        <initials>NC1</initials>
    </timekeeper>
    <date type="timecard">2015-09-12</date>
    <card-values>
        <card-value type="billed">
            <rate>325.00</rate>
            <hours>0.30</hours>
            <amount>97.5000</amount>
        </card-value>
    </card-values>
    <narrative>
        <line id="1">Narrative for 9/12/2015. With some more detail</line>
        <line id="2">detail on the 2nd line ID.</line>
    </narrative>
</timecard>

当前代码:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:aml="http://schemas.microsoft.com/aml/2001/core" 
    xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format" 
    xmlns:kmf="http://www.kleinmundo.com/functions" 
    xmlns:o="urn:schemas-microsoft-com:office:office" 
    xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" 
    xmlns:tlr="http://www.elite.com/functions" 
    xmlns:st1="urn:schemas-microsoft-com:office:smarttags" 
    xmlns:v="urn:schemas-microsoft-com:vml" 
    xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" 
    xmlns:w10="urn:schemas-microsoft-com:office:word" 
    xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" 
    xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template name="Time_Detail_Sample">
        <xsl:for-each select="xsm-sample/timecard">
            <w:p>
                <w:r>
                    <w:t>Date: <xsl:value-of select="format-date(date, '[M01]/[D01]/[Y01]')" />
                    </w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:r>
                    <w:t>Timekeeper: <xsl:value-of select="timekeeper/initials" />
                    </w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:r>
                    <w:t>Hours: <xsl:value-of select="format-number(card-values/card-value[@type='billed']/hours, '###,##0.00')" />
                    </w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:r>
                    <xsl:for-each select="narrative/line">
                        <w:t>
                            <xsl:value-of select="(.)" />
                            <xsl:value-of select="' '" />
                        </w:t>
                    </xsl:for-each>
                </w:r>
            </w:p>
            <w:p />
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

当前输出:

在此处输入图片说明

期望的输出:(如您所见,2015年9月1日的数据汇总为小时总和,所有“叙述”详细信息一并列出)。

在此处输入图片说明

我肯定会使用xsl:for-each-group ...

<xsl:for-each-group select="timecard"
  group-by="string-join((timekeeper/initials,date[@type='timecard']),'|')">
  <xsl:sort select="date[@type='timecard']"/>
  <w:p>
    <w:r>
      <w:t>
        <xsl:text>Date: </xsl:text>
        <xsl:value-of select="format-date(date, '[M01]/[D01]/[Y01]')"/>
      </w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:r>
      <w:t>
        <xsl:text>Timekeeper: </xsl:text>
        <xsl:value-of select="timekeeper/initials"/>
      </w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:r>
      <w:t>
        <xsl:text>Hours: </xsl:text>
        <xsl:value-of select="format-number(sum(current-group()/card-values/card-value[@type='billed']/hours), '###,##0.00')"/>
      </w:t>
    </w:r>
  </w:p>
  <w:p>
    <w:r>
      <w:t>
        <xsl:value-of select="current-group()/narrative/line" separator=" "/>
      </w:t>
    </w:r>
  </w:p>
  <w:p/>
</xsl:for-each-group>

我建议首先获取date所有不同值,然后将所有带有当前日期的时间timecard元素放入变量中,然后从该变量中提取所需的内容。 因此,类似:

<xsl:variable name="root" select="/"/>
<xsl:for-each select="distinct-values(//date[@type='timecard'])">
            <xsl:variable name="current_value" select="."/>
            <xsl:variable name="all_timecards_with_current_date" select="$root//timecard[descendant::date=$current_value]"/>
            <w:p>
                <w:r>
                    <w:t>Date: <xsl:value-of select="format-date($current_value, '[M01]/[D01]/[Y01]')" />
                    </w:t>
                </w:r>
            </w:p>

            <w:p>
                <w:r>
                    <w:t>Hours: <xsl:value-of select="sum($all_timecards_with_current_date//card-values/card-value[@type='billed']/hours)" />
                    </w:t>
                </w:r>
            </w:p>
            <w:p>
                <w:r>
                    <xsl:for-each select="$all_timecards_with_current_date//narrative/line">
                        <w:t>
                            <xsl:value-of select="." />
                        </w:t>
                    </xsl:for-each>
                </w:r>
            </w:p>

        </xsl:for-each>

不是很漂亮,当然不是最快的方法。 但是,我认为它比分组容易处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM