且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

基于生效日期和顺序创建 xslt 转换

更新时间:2022-12-10 08:00:41

假设:

  • 您可以使用 XSLT 2.0,
  • 输出应包含每个 Effective_Moment 的单独行(日期部分),对于每位员工,
  • you can use XSLT 2.0,
  • the output should contain separate row for each Effective_Moment (date part), for each employee,

您可以通过以下方式完成任务:

you can do the task the following way:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:peci="urn:com.w/peci" xpath-default-namespace="urn:com.w/peci">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:template match="Workers_Effective_Stack">
    <xsl:for-each-group select="Worker/Effective_Change" group-by=
      "concat(../Worker_Summary/Employee_ID, substring(Effective_Moment,1,10))">
      <xsl:value-of select="../Worker_Summary/Employee_ID"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="substring(Effective_Moment, 1, 10)"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="Worker_Status/Status"/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each-group>
  </xsl:template>
</xsl:transform>

注意:你的源 XML 有一个不平衡的 peci:Worker 开始标签,我删除了它们.

Caution: Your source XML has one unbalanced peci:Worker opening tag, I deleted them.

注意:可以单独按生效日期进行分组,但我认为不是你想要的.如果您的输入包含多个对不同员工的有效变更,但在同一生效日期,那么输出将只包含来自该组的一条记录.这就是我使用复合键进行分组的原因,还包括员工 ID.

Note: It is possible to perform grouping solely by the effective date, but I think is not what you want. If your input contained a number of effective changes for different employees, but on the same effective date, then the output would have contained only one record from this group. This is why I performed grouping using a composite key, including also the employee ID.