Jose L Martinez-Avial
Jose L Martinez-Avial

Reputation: 2265

Combine node atttributes from two lists

Based on Create list of nodes by copying from another lists, I would like to combine the attributes of a node in both lists. Let's say I have the following 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>

and I would like to combine both lists as in the question refered before, but taking the attributes from both lists. So in this case the output would be soemthing like this.

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

The field userPassword combines the attributes from vo/field[@name = 'userPassword' plus the attributes from input/field[@name = 'userPassword'. If an attribute is present in both input/field and vo/field, the value in input/field whould take precedence. For the field oldPasswordInQuotes there is no corresponding node in vo, so it should be copied as it is.

Upvotes: 0

Views: 145

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116957

This may actually be simpler than you think:

XSLT 1.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>

Upvotes: 1

Related Questions