Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 16th, 2018, 12:17 PM
Registered User
 
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>
 
Old August 16th, 2018, 05:54 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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
The Following User Says Thank You to mhkay For This Useful Post:
priyanka.kumar (August 17th, 2018)
 
Old August 17th, 2018, 01:43 AM
Registered User
 
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 01:45 AM..
 
Old August 17th, 2018, 03:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 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





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT Transformation Issue from source XML to Target XML Dhanunjay XSLT 1 July 17th, 2014 04:17 PM
XSLT - XML Transformation Performance fishstix XSLT 5 April 13th, 2009 01: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 02:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.