从Oracle创建XML RSS

时间:2012-11-07 16:41:37

标签: xml oracle

我要做的是创建一个存储过程,生成一个如下所示的xml。

    <?xml version="1.0" encoding="utf-8"?>
    <rss xmlns:georss="http://www.georss.org/georss" version="2.0">
    <rowset>
    <row>
      <title>myTitle</title>
      <description>myDescription</description>
      <georss:point>myLat myLon</georss:point>
     </row>
     ...
    </rowset>
    </rss>

我有以下内容生成

 SELECT DBMS_XMLGEN.getXML(q'!
 SELECT trim(sitedesc)  AS "title", 
    '<a href="../scripts/fmiinfo.asp?file_code=_BLDGLIST&sitecode='||trim(sitecode)||'&forceDB=">'||trim(siteabbrev)||'</a>'   AS "description", 
    trim(latitude)|| ' ' ||trim(longitude) AS "georss:point"
   FROM LS
   WHERE sold <> 1
    !') FROM dual;

输出看起来像这样

   <ROW>
   <title>Tipton Facilities</title>
   <description>&lt;a href=&quot;../scripts/fmiinfo.asp?file_code=_BLDGLISTnull=USINTI01null=&quot;&gt;TIP1&lt;/a&gt;</description>
   <georss:point>40.286008 -86.058054</georss:point>
   </ROW>

在'description'中我希望有一个指向另一个页面的链接,我理解xml有某些保留字符,如> < & %,看起来输出会自动将保留字符转换为它的实体引用。

然而问题是,程序认为网址中的sitecodeforcedb是变量,当它们不是时,它们会从结果中删除。

网址应为:&lt;a href="../scripts/fmiinfo.asp?file_code=_BLDGLIST&amp;sitecode=USINTI01&amp;forceDB="&gt;

但它是&lt;a href=&quot;../scripts/fmiinfo.asp?file_code=_BLDGLISTnull=USINTI01null=&quot;&gt;

我是否需要输入带ctr()功能的网址才能获得所需的输出?

SET SCAN OFF解决了我的变量问题 按照以下评论更新示例代码。

             SELECT XMLELEMENT("rss", XMLATTRIBUTES('http://www.georss.org/georss' AS "xmlns:georss"),
                XMLELEMENT("channel",
                             XMLAGG(XMLELEMENT("item",
                                               XMLFOREST(title AS "title",
                                                          description AS "description",
                                                          point AS "georss:point"))))).EXTRACT('/*')
             FROM (SELECT trim(longitude) || ' ' || trim(latitude) AS point, 
      '../scripts/fmiinfo.asp?file_code=_BLDGLIST&sitecode='||trim(sitecode)||'&forceDB=">'||trim(siteabbrev)||'</a>' AS description, 
      trim(sitedesc) AS title
      FROM ls 
      WHERE sold <>1
    );

1 个答案:

答案 0 :(得分:0)

你是从sqlplus运行的。默认情况下,sqlplus将&符号视为替换字符。要禁用此功能,请在sqlplus“set scan off”中运行。 GUI将有一个等效的设置来停止检查替代变量。

如果您使用XMLX函数(XMLForest,XMLElement),那么您的rss会更容易和更整洁。

例如

SQL> select xmlelement("rss", xmlattributes('http://www.georss.org/georss' as "xmlns:georss"),
  2                     xmlelement("rowset",
  3                                 xmlagg(xmlelement("row",
  4                                                    xmlforest(title as "title",
  5                                                               description as "description",
  6                                                               point as "georss:point"))))).extract('/*')
  7    from (select 'myLat myLon' point, 'asd' description, 'foo' title
  8             from dual
  9           union all
 10           select 'myLat2 myLon2' point,
 11                 '<a href="../scripts/fmiinfo.asp?file_code=_BLDGLIST&sitecode=USINTI01&forceDB=">' description,
 12                 'tetert' title
 13             from dual);

XMLELEMENT(RSS,XMLATTRIBUTES
------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
<rss xmlns:georss="http://www.georss.org/georss">
  <rowset>
    <row>
      <title>foo</title>
      <description>asd</description>
      <georss:point>myLat myLon</georss:point>
    </row>
    <row>
      <title>tetert</title>
      <description>&lt;a href=&quot;../scripts/fmiinfo.asp?file_code=_BLDGLIST&amp;sitecode=USINTI01&amp;forceDB=&quot;&gt;</description>
      <georss:point>myLat2 myLon2</georss:point>
    </row>
  </rowset>
</rss>


SQL>