具有属性和命名空间支持的Kohana提要

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了具有属性和命名空间支持的Kohana提要相关的知识,希望对你有一定的参考价值。

  1. <?
  2. class Feed2 extends feed {
  3. /**
  4. * Creates a feed from the given parameters.
  5. *
  6. * @param array feed information
  7. * @param array items to add to the feed
  8. * @param string define which format to use
  9. * @param string define which encoding to use
  10. * @return string
  11. */
  12. public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8')
  13. {
  14. $info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'Kohanaphp');
  15.  
  16. // handle namespaces
  17. $namespaceString = '';
  18.  
  19. if(isset($info['namespaces'])) {
  20. foreach($info['namespaces'] as $prefix => $ns) {
  21. // can't add namespaces using the api bc it adds in xmlns:xmlns="..." which is explicitly declared as invalid xml: http://www.w3.org/TR/REC-xml-names/#xmlReserved
  22. // $feed->addAttribute('xmlns:'.$prefix, $ns, 'http://www.w3.org/2000/xmlns/');
  23.  
  24. $namespaceString .= " xmlns:".$prefix.'="'.$ns.'"';
  25. }
  26.  
  27. $namespaces = $info['namespaces'];
  28.  
  29. // this is so it is not included in the rest of the $info generation
  30. unset($info['namespaces']);
  31. }
  32.  
  33. $feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"'.$namespaceString.'><channel></channel></rss>';
  34. $feed = simplexml_load_string($feed);
  35.  
  36. foreach ($info as $name => $value)
  37. {
  38. if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value)))
  39. {
  40. // Convert timestamps to RFC 822 formatted dates
  41. $value = date(DATE_RFC822, $value);
  42. }
  43. elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE)
  44. {
  45. // Convert URIs to URLs
  46. $value = url::site($value, 'http');
  47. }
  48.  
  49. // Add the info to the channel
  50. $feed->channel->addChild($name, $value);
  51. }
  52.  
  53. foreach ($items as $item)
  54. {
  55. // Add the item to the channel
  56. $row = $feed->channel->addChild('item');
  57.  
  58. foreach ($item as $name => $value)
  59. {
  60. if(!is_array($value)) {
  61. if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value)))
  62. {
  63. // Convert timestamps to RFC 822 formatted dates
  64. $value = date(DATE_RFC822, $value);
  65. }
  66. elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE)
  67. {
  68. // Convert URIs to URLs
  69. $value = url::site($value, 'http');
  70. }
  71.  
  72. // Add the info to the row
  73. $row->addChild($name, $value);
  74. } else {
  75. $child = $row->addChild($name);
  76.  
  77. // If the value is an array we are specifying attributes
  78. foreach($value as $attribute => $attributeValue) {
  79. if(strchr($attribute, ':') !== FALSE) {
  80. list($ns, $attribute2) = explode(':', $attribute);
  81. } else {
  82. $ns = "";
  83. }
  84.  
  85. //!empty($ns) ? $namespaces[$ns] : NULL
  86. $child->addAttribute($attribute, $attributeValue, !empty($ns) ? $namespaces[$ns] : NULL);
  87. }
  88. }
  89. }
  90. }
  91.  
  92. return $feed->asXML();
  93. }
  94. }
  95.  
  96. // Example Feed Generation (For Sparkle Update System):
  97.  
  98. $feedItems = array();
  99.  
  100. foreach(ORM::factory('updates')->find_all() as $clientUpdate) {
  101. $feedItems[] = array(
  102. 'title' => 'v'.$clientUpdate->version,
  103. 'pubDate' => $clientUpdate->date,
  104. 'enclosure' => array(
  105. 'url' => 'http://domain.com/',
  106. 'type' => 'application/octet-stream',
  107. 'length' => filesize('/path/to/file.zip'),
  108. 'sparkle:dsaSignature' => 'dsasig',
  109. 'sparkle:version' => '200',
  110. 'sparkle:shortVersionString' => '1.0',
  111. 'sparkle:minimumSystemVersion' => '10.6'
  112. )
  113. );
  114. }
  115.  
  116. $feedData = feed2::create(
  117. 'title' => 'Update Feed',
  118. 'author' => 'Michael Bianco',
  119. 'pubDate' => date('r'),
  120. 'link' => 'http://domain.com/',
  121. 'language' => 'en',
  122. 'namespaces' => array(
  123. 'sparkle' => 'http://www.andymatuschak.org/xml-namespaces/sparkle',
  124. 'dc' => 'http://purl.org/dc/elements/1.1/'
  125. )
  126. ),
  127. $feedItems
  128. );
  129.  
  130. ?>

以上是关于具有属性和命名空间支持的Kohana提要的主要内容,如果未能解决你的问题,请参考以下文章

使用命名空间时无法在 XSLT 中复制和修改属性

将带有命名空间和属性的 XML 转换为 C# 模型类

如何在具有默认命名空间的 xml 文档上使用 XPath

C++ 项目相关知识命名空间

XML和Schema命名空间详解

Tar 不保留“受信任”命名空间的扩展属性