アズールレーン | [A3] Thần Tinh Bảng | Espiritus

XML Name spaces

|
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
 22 views
of 13

Please download to get full document.

View again

Description
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>
Share
Transcript
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>
  • 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>
  • 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
  • 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>
  • 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>
  • 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
  • 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
  • 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>
  • 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>
  • 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
  • <?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> 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
  • 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>
  • Related Search
    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks