免责声明:我仍在学习 XML / XSLT,所以如果这很简单,我深表歉意。
我试图在节点 A 中查找一个元素,将其与节点 B 中的相似元素进行匹配,然后将一个条目从节点 B 返回到节点 A。
即找到 EmployeeTimeSheetEntry 下与 EmployeeTimeValuationResult/bookingDate 匹配的 startDate,并返回对应的 cust_Overtime_HR_Action 元素:
我正在尝试使用密钥来生成查找(基于此问题的答案:在另一个节点中查找时打印逗号分隔的标记),但我很不清楚该放在哪里apply-templates语句去检索节点(我现在很困惑,兜圈子)。
示例源 XML:
<EmployeeTimeSheet>
<EmployeeTimeSheet>
<employeeTimeSheetEntry>
<EmployeeTimeSheetEntry>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
<startDate>2024-11-25T00:00:00.000</startDate>
</EmployeeTimeSheetEntry>
<EmployeeTimeSheetEntry>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
<startDate>2024-11-27T00:00:00.000</startDate>
</EmployeeTimeSheetEntry>
<EmployeeTimeSheetEntry>
<cust_Overtime_HR_ACTION>Payout</cust_Overtime_HR_ACTION>
<startDate>2024-11-30T00:00:00.000</startDate>
</EmployeeTimeSheetEntry>
</employeeTimeSheetEntry>
<employeeTimeValuationResult>
<EmployeeTimeValuationResult>
<bookingDate>2024-11-25T00:00:00.000</bookingDate>
</EmployeeTimeValuationResult>
</employeeTimeValuationResult>
</EmployeeTimeSheet>
这就是我在 XSLT 中尝试(但失败)的事情:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- generate lookup table - must be top level, i.e. before template match -->
<xsl:key name="timeSheetEntry" match="EmployeeTimeSheet/EmployeeTimeSheet/employeeTimeSheetEntry/EmployeeTimeSheetEntry" use="startDate"/>
<xsl:template match="/">
<EmployeeTimeSheet>
<!-- retrieve cust_Overtime_HR_ACTION where the bookingDate matches the startDate in above lookup -->
<xsl:template match="/">
<xsl:apply-templates select="key('timeSheetEntry', EmployeeTimeSheet/EmployeeTimeSheet/employeeTimeValuationResult/EmployeeTimeValuationResult/bookingDate)/cust_Overtime_HR_ACTION"/>
</xsl:template>
<xsl:for-each select="EmployeeTimeSheet/EmployeeTimeSheet">
<xsl:for-each select="employeeTimeValuationResult/EmployeeTimeValuationResult">
<xsl:variable name="var_TimeValResult" select="./*"></xsl:variable>
<EmployeeTimeSheet>
<xsl:copy-of select="$var_TimeValResult"/>
<xsl:template match="cust_Overtime_HR_ACTION">
<overtimeHRaction>
<xsl:value-of select="."/>
</overtimeHRaction>
</xsl:template>
</EmployeeTimeSheet>
</xsl:for-each>
</xsl:for-each> <!-- EmployeeTimeSheet/EmployeeTimeSheet -->
</EmployeeTimeSheet>
</xsl:template>
(对初级代码表示歉意)
输出应如下所示,其中 2024-11-25 的元素 cust_Overtime_HR_ACTION 添加到 EmployeeTimeValuationResult 节点:
<EmployeeTimeSheet>
<EmployeeTimeSheet>
<employeeTimeValuationResult>
<EmployeeTimeValuationResult>
<bookingDate>2024-11-25T00:00:00.000</bookingDate>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
</EmployeeTimeValuationResult>
</employeeTimeValuationResult>
</EmployeeTimeSheet>
任何人都可以告诉我哪里出错了吗? XSLT 测试工具只是说:
无法使用提供的 XML/XSL 输入生成 XML 文档。样式表编译时报错
我无法弄清楚检索对应节点所需的逻辑。
提前非常感谢您提供的任何指导。
亲切的问候, 阿杰
考虑这个简化的示例:
XML
<EmployeeTimeSheet>
<employeeTimeSheetEntry>
<EmployeeTimeSheetEntry>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
<startDate>2024-11-25T00:00:00.000</startDate>
</EmployeeTimeSheetEntry>
<EmployeeTimeSheetEntry>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
<startDate>2024-11-27T00:00:00.000</startDate>
</EmployeeTimeSheetEntry>
<EmployeeTimeSheetEntry>
<cust_Overtime_HR_ACTION>Payout</cust_Overtime_HR_ACTION>
<startDate>2024-11-30T00:00:00.000</startDate>
</EmployeeTimeSheetEntry>
</employeeTimeSheetEntry>
<employeeTimeValuationResult>
<EmployeeTimeValuationResult>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>9</hours>
<externalCode>ad0a255f256d44ddbe41bc6ab1d632b9</externalCode>
<entityUUID>6CB3BB8B01C4434FABD28A84554B6A88</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-25T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
</EmployeeTimeValuationResult>
<EmployeeTimeValuationResult>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>9</hours>
<externalCode>ee32afbee0e1452fbdcc21e9ee9e45e5</externalCode>
<entityUUID>397C8AED082B4B5192941FF6D02C29FA</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-26T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
</EmployeeTimeValuationResult>
<EmployeeTimeValuationResult>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>6.5</hours>
<externalCode>6d84652c40bb49c7b028fda128802100</externalCode>
<entityUUID>73F775BAF8EA43F3BE011EB0C34601E5</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-27T00:00:00.000</bookingDate>
<hoursAndMinutes>6:30</hoursAndMinutes>
</EmployeeTimeValuationResult>
<EmployeeTimeValuationResult>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>9</hours>
<externalCode>1527b80a2ccf4c409e9549bb22b0da20</externalCode>
<entityUUID>1397BD090FC0477C8C9EABE8B15939D2</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-28T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
</EmployeeTimeValuationResult>
<EmployeeTimeValuationResult>
<timeTypeGroup>TFX_ABSENCES_TTG</timeTypeGroup>
<hours>2.5</hours>
<externalCode>e2092ce1a0c6498fadc6d4caa0836ef1</externalCode>
<entityUUID>A996867E64B9422FBC1765662286B633</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-27T00:00:00.000</bookingDate>
<hoursAndMinutes>2:30</hoursAndMinutes>
</EmployeeTimeValuationResult>
<EmployeeTimeValuationResult>
<timeTypeGroup>TFX_ABSENCES_TTG</timeTypeGroup>
<hours>9</hours>
<externalCode>50745ba2f6c241fc925a666aef955a50</externalCode>
<entityUUID>65D732D727E346B79E60318C4703910C</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-29T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
</EmployeeTimeValuationResult>
</employeeTimeValuationResult>
</EmployeeTimeSheet>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="k1" match="EmployeeTimeSheetEntry" use="startDate" />
<xsl:template match="/EmployeeTimeSheet">
<results>
<xsl:for-each select="employeeTimeValuationResult/EmployeeTimeValuationResult">
<result>
<xsl:copy-of select="*"/>
<xsl:copy-of select="key('k1', bookingDate)/cust_Overtime_HR_ACTION"/>
</result>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>9</hours>
<externalCode>ad0a255f256d44ddbe41bc6ab1d632b9</externalCode>
<entityUUID>6CB3BB8B01C4434FABD28A84554B6A88</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-25T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
</result>
<result>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>9</hours>
<externalCode>ee32afbee0e1452fbdcc21e9ee9e45e5</externalCode>
<entityUUID>397C8AED082B4B5192941FF6D02C29FA</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-26T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
</result>
<result>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>6.5</hours>
<externalCode>6d84652c40bb49c7b028fda128802100</externalCode>
<entityUUID>73F775BAF8EA43F3BE011EB0C34601E5</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-27T00:00:00.000</bookingDate>
<hoursAndMinutes>6:30</hoursAndMinutes>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
</result>
<result>
<timeTypeGroup>SCHED_WORK_TIME_A</timeTypeGroup>
<hours>9</hours>
<externalCode>1527b80a2ccf4c409e9549bb22b0da20</externalCode>
<entityUUID>1397BD090FC0477C8C9EABE8B15939D2</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-28T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
</result>
<result>
<timeTypeGroup>TFX_ABSENCES_TTG</timeTypeGroup>
<hours>2.5</hours>
<externalCode>e2092ce1a0c6498fadc6d4caa0836ef1</externalCode>
<entityUUID>A996867E64B9422FBC1765662286B633</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-27T00:00:00.000</bookingDate>
<hoursAndMinutes>2:30</hoursAndMinutes>
<cust_Overtime_HR_ACTION>TOIL</cust_Overtime_HR_ACTION>
</result>
<result>
<timeTypeGroup>TFX_ABSENCES_TTG</timeTypeGroup>
<hours>9</hours>
<externalCode>50745ba2f6c241fc925a666aef955a50</externalCode>
<entityUUID>65D732D727E346B79E60318C4703910C</entityUUID>
<EmployeeTimeSheet_externalCode>dd70365f71084e63b1143d4c40bd40a9</EmployeeTimeSheet_externalCode>
<bookingDate>2024-11-29T00:00:00.000</bookingDate>
<hoursAndMinutes>9:00</hoursAndMinutes>
</result>
</results>