如何在 XSLT Oxygen 中使用 Key [英] How to use Key in XSLT Oxygen

查看:32
本文介绍了如何在 XSLT Oxygen 中使用 Key的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XSLT 的关键概念基于 XML 中的两个单独节点获取输出我有以下 XML

<?xml version="1.0" encoding="UTF-8"?>
<mdti:Input xmlns:mdti="urn:com.workday/multiDocumentTransform/Input">
    <mdti:Files xmlns:mdti="urn:com.workday/multiDocumentTransform/Input">
        <mdti:EventFiles>
            <mdti:File mdti:filename="first.xml" mdti:contentType="text/xml">
                <wd:Report_Data xmlns:wd="urn:com.workday/bsvc">
                    <wd:Report_Entry>
                        <wd:key>1234</wd:key>
                        <wd:comp>ABC</wd:comp>
                        <wd:asof>2021-03-24T04:59:32.179-07:00</wd:asof>
                        <wd:emplid>33333333</wd:emplid>
                        <wd:worker_type>EMP</wd:worker_type>
                        <wd:emp_type>Regular</wd:emp_type>
                        <wd:orig_hire_dt>2021-11-27</wd:orig_hire_dt>
                        <wd:rehire_dt>2019-04-01</wd:rehire_dt>
                        <wd:home_host_class>M</wd:home_host_class>
                        <wd:service_dt>2014-11-27</wd:service_dt>
                    </wd:Report_Entry>
                </wd:Report_Data>
            </mdti:File>
            <mdti:File mdti:filename="second.xml" mdti:contentType="text/xml">
                <wd:Report_Data xmlns:wd="urn:com.workday/bsvc">
                    <wd:Report_Entry>
                        <wd:key>1234</wd:key>
                        <wd:supervisor_lname>xyz</wd:supervisor_lname>
                        <wd:hr_status>A</wd:hr_status>
                        <wd:hr_status_descr>Active</wd:hr_status_descr>
                        <wd:empl_status>A</wd:empl_status>
                        <wd:empl_status_descr>Active</wd:empl_status_descr>
                        <wd:ben_status>A</wd:ben_status>
                        <wd:home_address_change_dt>2019-07-30</wd:home_address_change_dt>
                        <wd:location>444</wd:location>
                        <wd:location_descr>Ind</wd:location_descr>
                    </wd:Report_Entry>
                </wd:Report_Data>
            </mdti:File>
        </mdti:EventFiles>
    </mdti:Files>
</mdti:Input>

**我在我的数据中使用以下 XSLT.如果我遗漏了什么以及如何去做,请告诉我.每个节点中唯一的标识符是 mdti:filename **

**I am using below XSLT for my data. Please let me know if I am missing something and way to do it. The only identifier in each node is the mdti:filename **

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:mdti="urn:com.workday/multiDocumentTransform/Input"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:wd="urn:com.workday/bsvc"
    exclude-result-prefixes="#all">
    
    <xsl:key name="share" match="mdti:Input/mdti:Files/mdti:EventFiles/mdti:File/wd:Report_Data/wd:Report_Entry" use="mdti:Input/mdti:Files/mdti:EventFiles/mdti:File/wd:Report_Data/wd:Report_Entry/wd:key"/> 
    
    <xsl:template match="/">        
        <data>
            <key>1234</key>
            <xsl:copy-of select="key('share', wd:key)/wd:hr_status"/>      
            <emp_type>Regular</emp_type>
            <supervisor_lname>xyz</supervisor_lname>
            <hr_status>A</hr_status>
            <location>444</location>
        </data>
    </xsl:template>    
</xsl:stylesheet>

推荐答案

你有几个错误.

首先, 将您置于根节点的上下文中;从这个上下文中,表达式 wd:key 什么都不选择 - 所以你的指令 <xsl:copy-of select="key('share', wd:key)/wd:hr_status";/> 什么都不做.

First, <xsl:template match="/"> puts you in the context of the root node; from this context, the expression wd:key selects nothing - so your instruction <xsl:copy-of select="key('share', wd:key)/wd:hr_status"/> does nothing.

接下来,如果您希望您的密钥匹配第二个文件中的节点,您应该将其限制为仅匹配第二个文件中的节点.此外,use 属性必须与匹配的节点相关.

Next, if you want your key to match nodes in the second file, you should restrict it to match only nodes in the second file. Also, the use attribute must be relative to the matched node.

此外,如果您希望结果在无命名空间中,则不能从输入复制节点 - 至少在 XSLT 1.0 中不能.

Furthermore, if you want the result to be in no-namespace, you cannot copy nodes from the input - at least not in XSLT 1.0.

还有更多,但这些是立即脱颖而出的.

There is more, but these are the ones that stand out immediately.

考虑以下示例:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mdti="urn:com.workday/multiDocumentTransform/Input"
xmlns:wd="urn:com.workday/bsvc"
exclude-result-prefixes="mdti wd">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
 
<xsl:key name="entry2" match="mdti:File[@mdti:filename='second.xml']/wd:Report_Data/wd:Report_Entry" use="wd:key"/> 
    
<xsl:template match="/mdti:Input">   
    <root> 
        <xsl:for-each select="mdti:Files/mdti:EventFiles/mdti:File[@mdti:filename='first.xml']/wd:Report_Data/wd:Report_Entry"> 
            <data>
                <!-- data from file1 -->    
                <key>
                    <xsl:value-of select="wd:key"/>      
                </key>
                 <emplid>
                    <xsl:value-of select="wd:emplid"/>      
                </emplid>
            
                <!-- data from file2 -->
                <xsl:variable name="entry2" select="key('entry2', wd:key)" />
                <supervisor_lname>
                    <xsl:value-of select="$entry2/wd:supervisor_lname"/>      
                </supervisor_lname>
                <hr_status>
                    <xsl:value-of select="$entry2/wd:hr_status"/>      
                </hr_status>
            
            </data>
        </xsl:for-each> 
    </root>  
</xsl:template> 
   
</xsl:stylesheet>

应用于您的输入示例,这将产生:

Applied to your input example, this will produce:

结果

<?xml version="1.0" encoding="utf-8"?>
<root>
  <data>
    <key>1234</key>
    <emplid>33333333</emplid>
    <supervisor_lname>xyz</supervisor_lname>
    <hr_status>A</hr_status>
  </data>
</root>

您可以根据需要从两个分支添加字段.

You can add fields from both branches as required.

这篇关于如何在 XSLT Oxygen 中使用 Key的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆