0

I have the below XML. I need to add an attribute to the first 'Entity' node. This attribute should have the value from the attribute node 'A-AssetFileName'. There are multiple Entity nodes within Data/Entities.

<?xml version="1.0" encoding="UTF-8"?>
<Entity Id="347" ExternalId="SSI1006" LongName="School Smart Hand Held Rustproof Carbon Steel Pencil Sharpener, Assorted Color, Pack of 24" EntityTypeId="18" EntityTypeName="Parent SKU" ContainerId="2" ContainerName="Collaboration Master" OrganizationName="DL" CategoryId="360" CategoryName="Sharpners" CategoryLongName="Sharpners" CategoryPath="SchoolSupplies»School_Office_Supplies»Sharpners" CategoryLongNamePath="" ParentEntityId="360" ParentExternalId="Sharpners" ParentExtensionEntityId="0" ParentExtensionEntityExternalId="" ParentExtensionEntityContainerId="0" ParentExtensionEntityContainerName="" ParentExtensionEntityCategoryId="0" ParentExtensionEntityCategoryPath="" ParentExtensionEntityCategoryLongNamePath="" Locale="en_WW" Action="Add">
<Attributes>
    <Attribute Id="4039" Name="Parent SKU #" LongName="Product Number" InstanceRefId="-1" Sequence="-1" AttributeParentId="4000" AttributeParentName="Core Attribute Group" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
        <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                <![CDATA[9-07466-030]]>
            </Value>
        </Values>
    </Attribute>
    <Attribute Id="4040" Name="Product Name" LongName="Product Name" InstanceRefId="-1" Sequence="-1" AttributeParentId="4000" AttributeParentName="Core Attribute Group" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
        <Values>
            <Value Id="0" Uom="" ValueRefId="-1" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                <![CDATA[School Smart Hand Held Rustproof Carbon Steel Pencil Sharpener]]>
            </Value>
        </Values>
    </Attribute>
   <Attribute Id="4122" Name="A-Assets" LongName="Assets" InstanceRefId="-1" Sequence="-1" AttributeParentId="4003" AttributeParentName="Media Attribute Group" AttributeType="ComplexCollection" AttributeDataType="String" Locale="en_WW" Action="Add">
        <Attributes>
            <Attribute Id="4122" Name="A-Assets Instance Record" LongName="Assets Instance Record" InstanceRefId="293" Sequence="0" AttributeParentId="4003" AttributeParentName="Media Attribute Group" AttributeType="Complex" AttributeDataType="String" Locale="en_WW" Action="Add">
                <Attributes>
                    <Attribute Id="4126" Name="A-AssetDescription" LongName="AssetDescription" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add" />
                        </Values>
                    </Attribute>
                    <Attribute Id="4127" Name="A-AssetDisplayName" LongName="AssetDisplayName" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add" />
                        </Values>
                    </Attribute>
                    <Attribute Id="4128" Name="A-AssetFileName" LongName="AssetFileName" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                                <![CDATA[SSI1006_SchoolSmart Sharpener_1.jpg]]>
                            </Value>
                        </Values>
                    </Attribute>
                    <Attribute Id="4129" Name="A-AssetID" LongName="AssetID" InstanceRefId="-1" Sequence="-1" AttributeParentId="4122" AttributeParentName="A-Assets" AttributeType="Simple" AttributeDataType="String" Locale="en_WW" Action="Add">
                        <Values>
                            <Value Id="-1" Uom="" ValueRefId="0" Sequence="-1" DisplayValue="" HasInvalidValue="False" Locale="en_WW" Action="Add">
                                <![CDATA[991E8697FD394A17A70C0DA7A73ECE87]]>
                            </Value>
                        </Values>
                    </Attribute>
                </Attributes>
            </Attribute>
        </Attributes>
    </Attribute>
</Attributes>
<Relationships />

2
  • refer : stackoverflow.com/questions/47092355/… OR stackoverflow.com/questions/11670712/… Commented Aug 15, 2018 at 10:38
  • the below is the XSLT I used. It doesn't work. <xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform"> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="Entity"> <xsl:copy> <xsl:attribute name='ProductImage'> <xsl:value-of select="//Attribute[@Name = A-AssetFileName]" /> </xsl:attribute> <xsl:apply-templates /> </xsl:copy> </xsl:template> </xsl:stylesheet> Commented Aug 16, 2018 at 10:56

1 Answer 1

0

There are couple of changes that need to be done for the template matching <Entity>.

You have mentioned that the attribute needs to be included only for the first <Entity> node. In that case the matching template will look like

<xsl:template match="Entity[1]">

When selecting the value for attribute @Name, the value should be enclosed in single quotes ' and additionally the XPath needs to be modified as below.

<xsl:value-of select="//Attribute[@Name = 'A-AssetFileName']/Values/Value" />

Lastly, when applying the other templates, you need to apply all nodes and attributes, else the attributes will not be applied.

<xsl:apply-templates select="@* | node()"/>

So the template will look like

<xsl:template match="Entity[1]">
    <xsl:copy>
        <xsl:attribute name='ProductImage'> 
            <xsl:value-of select="//Attribute[@Name = 'A-AssetFileName']/Values/Value" />
        </xsl:attribute>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.