namespace-handling.rst 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. .. _advanced.namespaces:
  2. Namespace Handling
  3. ==================
  4. :program:`Universal Feed Parser` attempts to expose all possible data in feeds,
  5. including elements in extension namespaces.
  6. Some common namespaced elements are mapped to core elements. For further
  7. information about these mappings, see :ref:`reference`.
  8. Other namespaced elements are available as ``prefixelement``.
  9. The namespaces defined in the feed are available in the parsed results as
  10. ``namespaces``, a dictionary of {prefix: namespaceURI}. If the feed defines a
  11. default namespace, it is listed as ``namespaces['']``.
  12. Accessing namespaced elements
  13. -----------------------------
  14. ::
  15. >>> import feedparser
  16. >>> d = feedparser.parse('http://feedparser.org/docs/examples/prism.rdf')
  17. >>> d.feed.prism_issn
  18. u'0028-0836'
  19. >>> d.namespaces
  20. {'': u'http://purl.org/rss/1.0/',
  21. 'prism': u'http://prismstandard.org/namespaces/1.2/basic/',
  22. 'rdf': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}
  23. The prefix used to construct the variable name is not guaranteed to be the same
  24. as the prefix of the namespaced element in the original feed. If
  25. :program:`Universal Feed Parser` recognizes the namespace, it will use the
  26. namespace's preferred prefix to construct the variable name. It will also list
  27. the namespace in the ``namespaces`` dictionary using the namespace's preferred
  28. prefix.
  29. In the previous example, the namespace
  30. (http://prismstandard.org/namespaces/1.2/basic/) was defined with the
  31. namespace's preferred prefix (prism), so the prism:issn element was accessible
  32. as the variable ``d.feed.prism_issn``. However, if the namespace is defined
  33. with a non-standard prefix, :program:`Universal Feed Parser` will still
  34. construct the variable name using the preferred prefix, *not* the actual prefix
  35. that is used in the feed.
  36. This will become clear with an example.
  37. Accessing namespaced elements with non-standard prefixes
  38. --------------------------------------------------------
  39. ::
  40. >>> import feedparser
  41. >>> d = feedparser.parse('http://feedparser.org/docs/examples/nonstandard_prefix.rdf')
  42. >>> d.feed.prism_issn
  43. u'0028-0836'
  44. >>> d.feed.foo_issn
  45. Traceback (most recent call last):
  46. File "<stdin>", line 1, in ?
  47. File "feedparser.py", line 158, in __getattr__
  48. raise AttributeError, "object has no attribute '%s'" % key
  49. AttributeError: object has no attribute 'foo_issn'
  50. >>> d.namespaces
  51. {'': u'http://purl.org/rss/1.0/',
  52. 'prism': u'http://prismstandard.org/namespaces/1.2/basic/',
  53. 'rdf': u'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}
  54. This is the complete list of namespaces that :program:`Universal Feed Parser`
  55. recognizes and uses to construct the variable names for data in these
  56. namespaces:
  57. =============== =====================================================
  58. Prefix Namespace
  59. =============== =====================================================
  60. admin http://webns.net/mvcb/
  61. ag http://purl.org/rss/1.0/modules/aggregation/
  62. annotate http://purl.org/rss/1.0/modules/annotate/
  63. audio http://media.tangent.org/rss/1.0/
  64. blogChannel http://backend.userland.com/blogChannelModule
  65. cc http://web.resource.org/cc/
  66. co http://purl.org/rss/1.0/modules/company
  67. content http://purl.org/rss/1.0/modules/content/
  68. cp http://my.theinfo.org/changed/1.0/rss/
  69. creativeCommons http://backend.userland.com/creativeCommonsRssModule
  70. dc http://purl.org/dc/elements/1.1/
  71. dcterms http://purl.org/dc/terms/
  72. email http://purl.org/rss/1.0/modules/email/
  73. ev http://purl.org/rss/1.0/modules/event/
  74. feedburner http://rssnamespace.org/feedburner/ext/1.0
  75. fm http://freshmeat.net/rss/fm/
  76. foaf http://xmlns.com/foaf/0.1/
  77. geo http://www.w3.org/2003/01/geo/wgs84_pos#
  78. icbm http://postneo.com/icbm/
  79. image http://purl.org/rss/1.0/modules/image/
  80. itunes http://example.com/DTDs/PodCast-1.0.dtd
  81. itunes http://www.itunes.com/DTDs/PodCast-1.0.dtd
  82. l http://purl.org/rss/1.0/modules/link/
  83. media http://search.yahoo.com/mrss
  84. pingback http://madskills.com/public/xml/rss/module/pingback/
  85. prism http://prismstandard.org/namespaces/1.2/basic/
  86. rdf http://www.w3.org/1999/02/22-rdf-syntax-ns#
  87. rdfs http://www.w3.org/2000/01/rdf-schema#
  88. ref http://purl.org/rss/1.0/modules/reference/
  89. reqv http://purl.org/rss/1.0/modules/richequiv/
  90. search http://purl.org/rss/1.0/modules/search/
  91. slash http://purl.org/rss/1.0/modules/slash/
  92. soap http://schemas.xmlsoap.org/soap/envelope/
  93. ss http://purl.org/rss/1.0/modules/servicestatus/
  94. str http://hacks.benhammersley.com/rss/streaming/
  95. sub http://purl.org/rss/1.0/modules/subscription/
  96. sy http://purl.org/rss/1.0/modules/syndication/
  97. szf http://schemas.pocketsoap.com/rss/myDescModule/
  98. taxo http://purl.org/rss/1.0/modules/taxonomy/
  99. thr http://purl.org/rss/1.0/modules/threading/
  100. ti http://purl.org/rss/1.0/modules/textinput/
  101. trackback http://madskills.com/public/xml/rss/module/trackback/
  102. wfw http://wellformedweb.org/CommentAPI/
  103. wiki http://purl.org/rss/1.0/modules/wiki/
  104. xhtml http://www.w3.org/1999/xhtml
  105. xlink http://www.w3.org/1999/xlink
  106. xml http://www.w3.org/XML/1998/namespace
  107. =============== =====================================================
  108. .. note::
  109. :program:`Universal Feed Parser` treats namespaces as case-insensitive to
  110. match the behavior of certain versions of :program:`iTunes`.
  111. .. warning::
  112. Data from namespaced elements is not :ref:`sanitized <advanced.sanitization>`
  113. (even if it contains :abbr:`HTML (HyperText Markup Language)` markup).