Xslt 合并两个列表中的节点属性

Xslt 合并两个列表中的节点属性,xslt,Xslt,基于,我想将两个列表中的节点属性组合起来。假设我有以下xml: <root> <vo> <field name="userLoginName" nameLDAP="uid" type="String"/> <field name="displayName" nameLDAP="displayName" type="String"/>

基于,我想将两个列表中的节点属性组合起来。假设我有以下xml:

<root>
    <vo>
        <field name="userLoginName"       nameLDAP="uid"              type="String"/>
        <field name="displayName"         nameLDAP="displayName"      type="String"/>
        <field name="firstName"           nameLDAP="givenName"        type="String"/>
        <field name="lastName"            nameLDAP="sn"               type="String"/>
        <field name="mail"                nameLDAP="mail"             type="String"/>
        <field name="userPassword"        nameLDAP="userPassword"     type="String" hidden="true"/>
        <field name="center"              nameLDAP="center"           type="String"/>
    </vo>
    <input>
        <field name="userPassword" mode="REPLACE"/>
        <field name="oldPasswordInQuotes" nameLDAP="unicodePwd"       type="byte[]" mode="ADD"/>
    </input>
</root>

我想像前面提到的问题一样合并两个列表,但是从两个列表中提取属性。所以在这种情况下,输出是这样的

<field name="userPassword" nameLDAP="userPassword" type="String" hidden="true" mode="REPLACE"/>
<field name="oldPasswordInQuotes" nameLDAP="unicodePwd" type="byte[]" mode="ADD"/>


字段userPassword组合了来自vo/field[@name='userPassword'的属性和来自input/field的属性[@name='userPassword'。如果在输入/字段和vo/字段中都存在属性,则输入/字段中的值将优先。对于字段oldPasswordInQuotes,vo中没有相应的节点,因此应按原样复制它。

这实际上可能比您想象的要简单:

XSLT1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name="vo" match="vo/field" use="@name" />

<xsl:template match="/">
    <xsl:for-each select="root/input/field">
        <xsl:copy>
            <xsl:copy-of select="key('vo', @name)/@*"/>
            <xsl:copy-of select="@*"/>
        </xsl:copy>
     </xsl:for-each>
</xsl:template>

</xsl:stylesheet>