You do not need to have a namespace declaration in the xml file. However, the issue with regards to the VA Smalltalk framework is that the rss
feed xml does not declare a namespace for itself, only for elements coming
from other namespaces. When the xml is parsed, no namespaceURI is
assigned to any of the elements with the exception of the slash:comments
element in the example below.
If you set a default namespaceURI for the parser, the xml elements will have the default namespace. Default namespaces have an empty string as a lookup key.
parser nameSpaceSupport declarePrefix: '' uri: 'rss20'.
Then you need to add the same namespaceURI to the mapping specification so that it will be correctly used for processing the xml.
<XmlMappingSpec Name="Rss20Mappings" NameSpaceURI="rss20" >
Once this is done, the deserialization framework can discriminate
between the two comments tags and invoke the correct selector specified
in the mapping specification.
Below is a small example based on Marten's earlier post. You will need to save the xml and the mapping specification as files on your system and use the class definitions from the earlier posts or create the classes yourself to see complete deserialization.
Hope that helps.
Diane
Workspace:
| mappingDom mappingSpec orderObject parser|
"my workspace"
"Create a mapping specification from the mapping XML"
mappingDom := AbtXmlDOMParser newValidatingParser parseURI: 'C:\aaVastDev2011\MartenXmlProb\martenExample.map'.
mappingSpec := AbtXmlMappingSpec fromMappingDOM: mappingDom.
"mappingSpec inspect."
parser := AbtXmlMappingParser new mappingSpec: mappingSpec.
"declare a default namespace prefix which is an empty string so that
the rss feed xml has a namespaceURI assigned to it in VA Smalltalk"
parser nameSpaceSupport declarePrefix: '' uri: 'rss20'.
orderObject := parser parseURI: 'C:\aaVastDev2011\MartenXmlProb\martenXml.xml'.
orderObject inspect.
orderObject abtXmlMappedObject inspect
Xml:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
>
<channel>
<title>blog.tagesschau.de</title>
<generator>http://wordpress.org/v=3.1.2</generator>
<item>
<title>Mecklenburg-Vorpommern</title>
<comments>http://blog.tagesschau.de/2011/08/31/mecklenburg-vorpommern-gehen-oder-bleiben/#comments</comments>
<slash:comments>12</slash:comments>
</item>
</channel>
</rss>
Mapping Specification:
<?xml version="1.0"?>
<!DOCTYPE XmlMappingSpec SYSTEM "abtxmap.dtd">
<XmlMappingSpec Name="Rss20Mappings" NameSpaceURI="rss20" >
<ClassElementMapping ElementTagName="rss" ClassName="RssSpecification">
<AttributeMapping ClassAttribute="channel" >
<SubElement>channel</SubElement>
</AttributeMapping>
</ClassElementMapping>
<ClassElementMapping ElementTagName="channel" ClassName="RssChannel">
<AttributeMapping ClassAttribute="title" >
<Attribute>title</Attribute>
</AttributeMapping>
<AttributeMapping ClassAttribute="items" >
<SubElement>item</SubElement>
</AttributeMapping>
</ClassElementMapping>
<ClassElementMapping ElementTagName="item" ClassName="RssChannelItem">
<AttributeMapping ClassAttribute="title" >
<Attribute>title</Attribute>
</AttributeMapping>
<AttributeMapping
ClassAttribute="numberOfComments"
NameSpaceURI="http://purl.org/rss/1.0/modules/slash/" >
<Attribute>comments</Attribute>
</AttributeMapping>
<AttributeMapping
ClassAttribute="comments" >
<Attribute>comments</Attribute>
</AttributeMapping>
</ClassElementMapping>
</XmlMappingSpec>