Click to See Complete Forum and Search --> : XSLT and attributes
seamus123
04-15-2003, 07:33 AM
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.
khalidali63
04-15-2003, 07:48 AM
You should provide with the code snippet that is creating the attributes....
Cheers
Khalid
seamus123
04-15-2003, 08:17 AM
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>
khalidali63
04-15-2003, 08:49 AM
Well here is the XML file. I took the liberty to reformat it, and lowercased all the element names
:D
<?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
<?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
<?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/
seamus123
04-15-2003, 10:01 AM
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.
khalidali63
04-15-2003, 10:09 AM
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
seamus123
04-15-2003, 02:17 PM
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>
seamus123
04-15-2003, 02:48 PM
I finally got it!
Thanks for your help Khalid!
S.
khalidali63
04-15-2003, 02:53 PM
No problem...and glad to be of any help
Cheers
Khalid