Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 16th, 2018, 01:17 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default XML Transformation using XSLT

I have the below sample 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.

Code:
<Data Schema="RSXML4.0" xmlns="http://schemas.riversand.com/mdmcenter"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://schemas.riversand.com/mdmcenter/RSXml4.0.xsd">
    <Entities>
        <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="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"><![CDATA[]]></Value>
                                    </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"><![CDATA[]]></Value>
                                    </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>
                            </Attributes>
                        </Attribute>
                    </Attributes>
                </Attribute>
                <Attribute Id="4619" Name="ItemColor" LongName="ItemColor" 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[Assorted]]></Value>
                    </Values>
                </Attribute>
            </Attributes>
            <Relationships/>
        </Entity>
    </Entities>
</Data>
My XSLT is as below. The attribute does not get added, no matter what I try. :(

Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/Data/Entities/Entity">
        <xsl:copy>
            <xsl:attribute name="ProductImage">
                123 <!-- trying to add atleast a static data first -->
<!--                <xsl:value-of select="//Attribute[@Name = A-AssetFileName]" />-->
            </xsl:attribute>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
Reply With Quote
  #2 (permalink)  
Old August 16th, 2018, 06:54 PM
mhkay's Avatar
Wrox Author
Points: 18,420, Level: 59
Points: 18,420, Level: 59 Points: 18,420, Level: 59 Points: 18,420, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,952
Thanks: 0
Thanked 289 Times in 284 Posts
Default

The elements in your source XML are in a namespace, but your stylesheet is matching no-namespace element names. You need

Code:
<xsl:template match="/x:Data/x:Entities/x:Entity">
with the namespace declaration xmlns:x="http://schemas.riversand.com/mdmcenter"; and similarly for select expressions that use element names.

One of the most common mistakes for XSLT beginners, I'm afraid.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
priyanka.kumar (August 17th, 2018)
  #3 (permalink)  
Old August 17th, 2018, 02:43 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2018
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you very much! this worked like a charm to add the attribute.
However, I tried adding the value of node 'attribute' with name 'A-AssetFileName' to that attribute, but it always shows blank.

Code:
 <xsl:value-of select="//@x:Attribute[Name = A-AssetFileName]" />
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.riversand.com/mdmcenter" exclude-result-prefixes="msxsl">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/x:Data/x:Entities/x:Entity">
        <xsl:copy>
            <xsl:attribute name="ProductImage">
                <xsl:value-of select="//@x:Attribute[Name = A-AssetFileName]" />
            </xsl:attribute>
            <xsl:apply-templates select = "@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
also, how do I retain the CDATA values as it is, which are present within the nodes.

Last edited by priyanka.kumar; August 17th, 2018 at 02:45 AM.
Reply With Quote
  #4 (permalink)  
Old August 17th, 2018, 04:17 AM
mhkay's Avatar
Wrox Author
Points: 18,420, Level: 59
Points: 18,420, Level: 59 Points: 18,420, Level: 59 Points: 18,420, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,952
Thanks: 0
Thanked 289 Times in 284 Posts
Default

Please don't ask new questions on an existing thread.

Despite its name, Attribute is not an attribute, it is an element. And Name is an attribute, and A-AssetFileName is a string literal.

And you don't want "//X" because that selects every X in the document. So you want

Code:
x:Attribute[@Name = "A-AssetFileName"]
You're getting a lot of very basic things wrong, so it might be a good idea to read a few more chapters of your Wrox book before you do any more coding.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Transformation Issue from source XML to Target XML Dhanunjay XSLT 1 July 17th, 2014 05:17 PM
XSLT - XML Transformation Performance fishstix XSLT 5 April 13th, 2009 02:48 PM
xml schema transformation using xslt xsltbeginner XSLT 1 March 3rd, 2009 10:11 AM
Regarding xml-html transformation of an xml string using xslt and javascript suprakash444 XSLT 1 January 12th, 2009 01:23 AM
XSLT transformation from XML buffer and XSL file sundaramkumar Javascript 1 September 5th, 2005 03:11 AM



All times are GMT -4. The time now is 08:31 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.