AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79248221
Accepted
AJW
AJW
Asked: 2024-12-03 23:30:17 +0800 CST2024-12-03 23:30:17 +0800 CST 2024-12-03 23:30:17 +0800 CST

XSLT 匹配另一个 XML 节点中的条目并返回相应的元素

  • 772

免责声明:我仍在学习 XML/XSLT,因此如果这太简单的话请原谅。

我正在尝试在节点 A 中查找一个元素,将其与节点 B 中的类似元素匹配,并将从节点 B 的条目返回到节点 A。

即找到与EmployeeTimeValuationResult/bookingDate匹配的EmployeeTimeSheetEntry下的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> 

</xsl:样式表>

(对于初级水平的代码,深表歉意)

输出应如下所示,其中 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 文档。样式表编译期间报告了错误

我无法弄清楚检索相应节点所需的逻辑。

非常感谢您提供的任何指导。

谨致问候,AJ

xml
  • 1 1 个回答
  • 24 Views

1 个回答

  • Voted
  1. Best Answer
    michael.hor257k
    2024-12-04T00:34:51+08:002024-12-04T00:34:51+08:00

    考虑这个简化的例子:

    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>
    
    • 1

相关问题

  • XSL 替换单个元素的命名空间 URI,但保留前缀

  • 使用 xslt 替换 xml 文件中元素中的值

  • XML - 将列转换为单独的行?

  • 如何在 Oxygen 中通过 Saxon 正确运行 SQL 扩展

  • XML 架构:元素 A、元素 B 或两者

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve