www.webdeveloper.com
Results 1 to 9 of 9

Thread: XSLT and attributes

  1. #1
    Join Date
    Mar 2003
    Posts
    9

    Question XSLT and attributes

    I am using an xslt to transform an xml document into another format xml doc.

    Transform this:

    <Place_Name>
    <Name>THE PLACE</Name>
    <Name_Type_ID>1</Name_Type_ID>
    - <Name_Type>
    <Name_Type_ID>1</Name_Type_ID>
    <Type>Legal</Type>
    </Name_Type>
    </Place_Name>

    Into this:

    <Names>
    <Name Language="English" Type ="Legal">THE PLACE</Name>
    </Names>


    I run into problems when trying to define attributes in the Name element. Does anyone know how to produce the result I want using an xslt document?

    Regards, S.

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    You should provide with the code snippet that is creating the attributes....

    Cheers

    Khalid

  3. #3
    Join Date
    Mar 2003
    Posts
    9
    Here is the xslt I am using to produce part of the result I want. I need to know how to produce the "Type" attribute that will contain the value of the original xml document.

    <xsl:for-each select="Place_Name">
    <xsl:for-each select="Name">
    <Names>
    <Name>

    <xsl:attribute name="Language">English</xsl:attribute>
    <xsl:apply-templates/>

    </Name>
    </Names>
    </xsl:for-each>
    </xsl:for-each>

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    Well here is the XML file. I took the liberty to reformat it, and lowercased all the element names



    Code:
    <?xml version='1.0'?>
    <place_name>
    	<name>
    	<name_value>THE PLACE</name_value>
    	<name_type>
    		<name_type_id>1</name_type_id>
    		<language>English</language>
    		<type>Legal</type>
    	</name_type>
    	</name>
    </place_name>
    and here is the xsl code toformat the above xml

    Code:
    <?xml version='1.0'?>
    <xsl:stylesheet
    			version = "1.0"
             xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml"/>
    	<xsl:template match="name">
    	<names>
    		<name>
    			<xsl:attribute name="language">
    				<xsl:value-of select="name_type/language"/>
    			</xsl:attribute>
    			<xsl:attribute name="type">
    				<xsl:value-of select="name_type/type"/>
    			</xsl:attribute>
    			<xsl:value-of select="name_value"/>
    		</name>
    	</names>
    	</xsl:template>
    </xsl:stylesheet>
    I have tested the above which produced this result

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
     <names>
        <name language="English" type="Legal">THE PLACE
        </name>
    </names>
    I hope this helps

    Cheers

    Khalid

    P.S
    For hardcore xslt help visit
    http://www.mulberrytech.com/xsl/xsl-list/

  5. #5
    Join Date
    Mar 2003
    Posts
    9

    Smile

    It definitley works Khalid, thanks.

    I'm still having trouble integrating it into my much larger document for some reason.

    Is there any reason why you match "name" as the template and not "place_name"?

    S.

  6. #6
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    since I did not have the much larger xml doc infront of me,the patial xml you posted,did not look a logical format to me..

    :-)

    I created a block of elements that will be applied xsl on.sub root element <name> is such block that contains a number of other elements.

    Make any sense????

    Khalid

  7. #7
    Join Date
    Mar 2003
    Posts
    9

    Unhappy

    I am still plagued with an "Unexpected Child" error.

    When trying to integrate the code you provided.
    Here's is my entire document. Do you know what this error means?

    <?xml version='1.0'?>
    <Import>
    <Place_Master>
    <Place_ID>1</Place_ID>
    <Location_ID>20</Location_ID>
    <Official_Name>Military Compound</Official_Name>
    <Official_Name_F>Quartier militaire</Official_Name_F>
    <Locations>
    <Location>Fredericton</Location>
    <Location_ID>20</Location_ID>
    </Locations>
    <Place_Name>
    <Place_ID>1</Place_ID>
    <Name>THE PLACE</Name>
    <Name_F>LA PLACE</Name_F>
    <Name_Type_ID>1</Name_Type_ID>
    <Name_Type>
    <Name_Type_ID>1</Name_Type_ID>
    <Type>Legal </Type>
    <Type_F>Juridique</Type_F>
    </Name_Type>
    </Place_Name>
    </Place_Master>
    </Import>

  8. #8
    Join Date
    Mar 2003
    Posts
    9
    I finally got it!

    Thanks for your help Khalid!

    S.

  9. #9
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    2,653
    No problem...and glad to be of any help

    Cheers

    Khalid

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles