简体   繁体   English

XSLT 1.0 分组数据集,包括不同元素和 0 个或多个重复元素

[英]XSLT 1.0 Grouping Dataset including distinct elements and 0 or more repeated elements

I am trying to write some XSLT that groups the XML data based on certain keyed fields.我正在尝试编写一些 XSLT 根据某些键字段对 XML 数据进行分组。 These fields include some static fields as well as the potential of additional fields.这些字段包括一些 static 字段以及潜在的附加字段。

Data Example:数据示例:

<ReportResult>
  <ReportHeader>
    <Header>Device Serial #</Header>
    <Header>Device Type</Header>
    <Header>Site ID</Header>
    <Header>Site Name</Header>
    <Header>Operation</Header>
    <Header>Option1</Header>
    <Header>Option 4</Header>
  </ReportHeader>
  <ReportData>
    <DataEntity>
      <kbserno>31513766</kbserno>
      <DeviceProductDesc>ExampleProductName</DeviceProductDesc>
      <SiteID>ExampleSiteID</SiteID>
      <SiteName>ExampleSiteName</SiteName>
      <OperationCodeDesc>Open</OperationCodeDesc>
      <KeyholderOption />
      <KeyholderOption />
    </DataEntity>
    <DataEntity>
      <kbserno>31513766</kbserno>
      <DeviceProductDesc>ExampleProductName</DeviceProductDesc>
      <SiteID>ExampleSiteID</SiteID>
      <SiteName>ExampleSiteName</SiteName>
      <OperationCodeDesc>Open</OperationCodeDesc>
      <KeyholderOption />
      <KeyholderOption />
    </DataEntity>
  </ReportData>
</ReportResult>

As it stands, I have code that groups based on the kbserno , SiteID , and OperationCodeDesc :就目前而言,我有基于kbsernoSiteIDOperationCodeDesc分组的代码:

<xsl:for-each select="//DataEntity[generate-id(.) = generate-id(key('keyActivitySummary', concat(kbserno,'+',SiteID,'+',OperationCodeDesc))[1])]">
                
              <xsl:variable name="lngKbserno"><xsl:value-of select="kbserno" /></xsl:variable>
              <xsl:variable name="lngSite"><xsl:value-of select="SiteID" /></xsl:variable>
              <xsl:variable name="lngOperation"><xsl:value-of select="OperationCodeDesc" /></xsl:variable>
                
                
               <!-- Select all the entries that belong to that group -->
               <!--Create template to create select statement below dynamically based on groupingheaders-->
               <xsl:variable name="GroupList" select="//DataEntity[kbserno=$lngKbserno and 
                                                                OperationCodeDesc=$lngOperation and
                                                                  SiteID=$lngSite]" />
               <xsl:for-each select="$GroupList[1]/*">
                 <!-- Do stuff to the grouping -->
               </xsl:for-each>
</xsl:for-each>

Basically, I need to add it such that when KeyholderOption tags are present in the date (as in the data given), the date is additionally grouped on the first KeyholderOption present in each data entity, the second KeyholderOption present, etc.基本上,我需要添加它,以便当 KeyholderOption 标签存在于日期中(如给定的数据中),日期另外分组在每个数据实体中存在的第一个 KeyholderOption 上,第二个 KeyholderOption 存在等。

Additionally, the values in these KeyholderOption entities can be NULL (as shown).此外,这些 KeyholderOption 实体中的值可以是 NULL(如图所示)。 However, all entities will have the same number of these entries (ie if an entity has 2 KeyholderOptions, then all entities will have 2).但是,所有实体将具有相同数量的这些条目(即,如果一个实体有 2 个 KeyholderOptions,那么所有实体将有 2 个)。

I have just tried adding KeyholderOption to the key in the code above, but that just results in none of the data being output properly.我刚刚尝试将 KeyholderOption 添加到上面代码中的键中,但这只会导致没有正确的数据是 output。

This would be the first transform to add the element.这将是添加元素的第一个转换。 Then you can use the Muenchian grouping method for the second transform and get rid of the key, if needed.然后,如果需要,您可以使用 Muenchian 分组方法进行第二次变换并去掉密钥。 Play with the key to get it how you want it.用钥匙玩你想要的。

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

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

    <xsl:element name="key">
      <xsl:value-of select="concat(kbserno, '^^')"/>
      <xsl:value-of select="concat(SiteID, '^^')"/>
      <xsl:value-of select="concat(OperationCodeDesc, '^^')"/>
      <xsl:for-each select="KeyholderOption">
        <xsl:value-of select="concat(., '^^')"/>
      </xsl:for-each>
    </xsl:element>
    
  </xsl:copy>
</xsl:template>

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

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