1 / 13

XML Name spaces

XML Name spaces. Different people may invent similar tag names. Here is an XML element describing a piece of furniture: <table> <name>table99</name> <type>dining table</type> <width>80</width> <length>180</length> </table>

Download Presentation

XML Name spaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. XML Name spaces

  2. Different people may invent similar tag names • Here is an XML element describing a piece of furniture: <table> <name>table99</name> <type>dining table</type> <width>80</width> <length>180</length> </table> • Here is an XML element describing a collection of data: <table> <person> <name>Bertie Ahern</name> <type>male</type> </person> <person> <name>Mary Harney</name> <type>female</type> </person> </table>

  3. Distinguishing tags with similar names • Sometimes an application will involve multiple documents, each containing tags that have the same names but different meanings • We can distinguish between similar tag names by using prefixes

  4. Distinguishing tags with similar names (contd.) • The XML element describing a piece of furniture: <furniture:table> <furniture:name>table99</furniture:name> <furniture:type>dining table</furniture:type> <furniture:width>80</furniture:width> <furniture:length>180</furniture:length> </furniture:table> • The XML element describing a collection of data: <data:table> <data:person> <data:name>Bertie Ahern</data:name> <data:type>male</data:type> </data:person> <data:person> <data:name>Mary Harney</data:name> <data:type>female</data:type> </data:person> </data:table>

  5. Distinguishing tags with similar names (contd.) • But two different software engineers might even still choose similar prefixes! • Extract from one application: <furniture:table> <furniture:name>table99</furniture:name> <furniture:type>dining table</furniture:type> <furniture:width>80</furniture:width> <furniture:length>180</furniture:length> </furniture:table> • Extract from another application: <furniture:table> <furniture:price>199</furniture:price> <furniture:status>second-hand</furniture:status> </furniture:table>

  6. Distinguishing tags with similar names (contd.) • We avoid ambiguity by associating each prefix with something that must be unique • a Universal Resource Identifier • The association between a prefix and a URI is made using a special XML attribute • the XML Namespace (xmlns) Attribute • Format of the xmlns attribute: xmlns:prefix="URI” • Example usages: <furniture:table xmlns:furniture=“http://abc.com”> <furniture:table xmlns:furniture=“http://abracadabra.co.uk”> • By the way: never invent your own tag/attribute names starting with “xml…” • these are reserved for W3C usage

  7. Distinguishing tags with similar names (contd.) • When a namespace is defined in the start tag of an element, all child elements with the same prefix are associated with the same namespace • Example <furniture:table xmlns:furniture=“http://abc.com” > <furniture:name>table99</furniture:name> <furniture:type>dining table</furniture:type> <furniture:width>80</furniture:width> <furniture:length>180</furniture:length> </furniture:table> • In this case, all the furniture prefixes are associated with the http://abc.com URI

  8. Some more examples • A different furniture example: <furniture:table xmlns:furniture=“http://abracadabra.co.uk”> <furniture:price>199</furniture:price> <furniture:status>second-hand</furniture:status> </furniture:table> • A personnel data example <data:table xmlns:data=“http://irlgov.ie”> <data:person> <data:name>Bertie Ahern</data:name> <data:type>male</data:type> </data:person> <data:person> <data:name>Mary Harney</data:name> <data:type>female</data:type> </data:person> </data:table>

  9. Default Namespaces • We can reduce the need for prefixes if we define a default namespace for an element • All tag names in descendant elements are then understood as implicitly having the same namespace as that for the ancestor element • The format for a default namespace usage is <someTagName xmlns=“someURI” > … </someTagName> • Example usage: <furniture:table xmlns:furniture=“http://abracadabra.co.uk”> <furniture:price>199</furniture:price> <furniture:status>second-hand</furniture:status> </furniture:table> is equivalent to: <table xmlns=“http://abracadabra.co.uk”> <price>199</price> <status>second-hand</status> </table>

  10. Example usage of XML namespaces • A frequent usage of namespaces is in XSL stylesheets • There is an example on the next slide • The XSL stylesheet transforms a particular form of XML document into XHTML • Thus many of the tags in the stylesheet are XHTML tags • But many tags are are XSL tags • they are identified as such by having the xsl prefix • and the semantics of the xsl prefix are defined by associating it with a W3C URI

  11. <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform” version="1.0"> <xsl:template match="/"> <html><head><title>Data</title></head> <body><table rules="all"> <thead><tr><th>Name</th><th>Sex</th></tr></thead> <tbody> <xsl:apply-templates/> </tbody></table></body></html> </xsl:template> <xsl:template match="person"> <tr><xsl:apply-templates/></tr> </xsl:template> <xsl:template match="male"> <td><xsl:value-of select="."/></td><td>male</td> </xsl:template> <xsl:template match="female"> <td> <xsl:value-of select="."/> </td><td>female</td> </xsl:template> </xsl:transform>

  12. An XSL application where there would have been tagname clashes • Consider the following XML fragment: <table xmlns=“http://abc.com” > <name>table99</name> <type>dining table</type> <width>80</width> <length>180</length> </table> • Supposewe wanted to use XSL to transform elements like this into elements like this <table xmlns=“http://abc.com” > <name>table99</name> <template>dining table</template> <width>80</width> <length>180</length> </table> • That is, we are replacing an application-specific element having the tagname type with one having the tagname template

  13. An XSL application with potential tagname clash (contd.) • Our XSL stylesheet would have two kinds of template element • one from the XSL language • another from the application-specific furniture language • We would use the xsl prefix to distinguish one of these from the other • Here is an extract from the relevant XML stylesheet: <?xml version="1.0"?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform” version="1.0"> ... <xsl:template match="type"> <template><xsl:value-of select="."/></template> </xsl:template> ... <xsl:transform>

More Related