【问题标题】:current-group() except for one element in xsltcurrent-group() 除了 xslt 中的一个元素
【发布时间】:2017-11-13 16:05:57
【问题描述】:

以下是我的输入 xml。我正在尝试使用 current-group() 函数进行分组,但它不符合我的要求,下面我提供了详细信息。

        <UsrTimeCardEntry>
            <Code>1<Code>
            <Name>TC1</Name>
            <Person>
                <Code>074</Code>
            </Person>
        </UsrTimeCardEntry>
        <UsrTimeCardEntry>
            <Code>2<Code>
            <Name>TC2</Name>
            <Person>
                <Code>074</Code>
            </Person>
        </UsrTimeCardEntry>

我想按人/代码对其进行分组,使其看起来像这样

   <Person Code="074">
       <UsrTimeCardEntry>
              <Code>1</Code>
              <Name>TC1</Name>
       </UsrTimeCardEntry>
       <UsrTimeCardEntry>
              <Code>2</Code>
              <Name>TC2</Name>
       </UsrTimeCardEntry>
</Person>

为此,我使用了下面的 xslt,但它再次复制了我不想要的人,我在这里缺少什么,我尝试使用 current-group() except 而不是 [child: :Person] 但这也不起作用。

<xsl:template match="businessobjects">
    <xsl:for-each-group select="UsrTimeCardEntry" group-by="Person/Code">
        <Person Code="{current-grouping-key()}">
            <xsl:copy-of select="current-group()"></xsl:copy-of>
        </Person>
    </xsl:for-each-group>
</xsl:template>

【问题讨论】:

    标签: xslt grouping except


    【解决方案1】:

    这里不使用xsl:copy-of,而是使用xsl:apply-templates,然后可以添加模板忽略Person节点

    <xsl:template match="Person" />
    

    这假设您也在使用身份模板正常复制所有其他节点。

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

    试试这个 XSLT

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
        <xsl:output method="xml" indent="yes" />
    
        <xsl:strip-space elements="*" />
    
        <xsl:template match="businessobjects">
            <xsl:for-each-group select="UsrTimeCardEntry" group-by="Person/Code">
                <Person Code="{current-grouping-key()}">
                    <xsl:apply-templates select="current-group()" />
                </Person>
            </xsl:for-each-group>
        </xsl:template>
    
        <xsl:template match="Person" />
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 2019-08-01
      • 1970-01-01
      相关资源
      最近更新 更多