xmlforest中未转义的列值

时间:2012-10-31 16:10:10

标签: xml oracle

我有一个表列,用于存储像

这样的xml块
<Text>hello, world</Text></Image id="100">

或没有标签的纯文本。如何在没有转义符号的情况下从标记的列值生成xml。

这是一个声明的例子:

select xmlelement("Proposal", xmlforest(1 as "ProposalType"
                                   ,to_char(sysdate, 'dd.mm.yyyy') as "CreateDate"
                                   ,'title1' as "Title"
                                   ,'<Text>hello, world</Text></Image id="100">' as "InfoBlock"))
  from dual;

生成的xml如下所示:

<Proposal>
    <ProposalType>1</ProposalType>
    <CreateDate>31.10.2012</CreateDate>
    <Title>title1</Title>
   <InfoBlock2>&lt;Text&gt;hello, world&lt;/Text&gt;&lt;/Image id=&quot;100&quot;&gt;</InfoBlock2>
</Proposal>

但我需要像这样的xml:

<Proposal>
    <ProposalType>1</ProposalType>
    <CreateDate>31.10.2012</CreateDate>
    <Title>title1</Title>
    <InfoBlock2>
        <Text>hello, world</Text><Image id="100"/>
    </InfoBlock2>
</Proposal>

2 个答案:

答案 0 :(得分:1)

这是我能做的最好的事情:

  select xmlelement("Proposal",xmlelement("ProposalType",1)
                              ,xmlelement("CreateDate",to_char(sysdate, 'dd.mm.yyyy'))
                              ,xmlelement("Title",'Title1')
                              ,xmlelement("InfoBlock",xmlelement("Text",'Hello World')
                                                     ,xmlelement("Image",xmlattributes(10 as "Id"))
                                         )
                   )           
  from dual;

结果:

<Proposal>
    <ProposalType>1</ProposalType>
    <CreateDate>31.10.2012</CreateDate>
    <Title>Title1</Title>
    <InfoBlock>
        <Text>Hello World</Text>
        <Image Id="10"></Image>
    </InfoBlock>
</Proposal>

这给出了

<Image Id="10"></Image>

不知道该怎么做

<Image id="10"/>

答案 1 :(得分:1)

SQL> select xmlelement("Proposal",
  2                     xmlforest(1 as "ProposalType",
  3                               to_char(sysdate, 'dd.mm.yyyy') as "CreateDate",
  4                              'title1' as "Title"
  5                              ),
  6                     xmltype('<InfoBlock><Text>hello, world</Text><Image id="100"/></InfoBlock>'))
  7             .extract('/*') -- not needed, just put to pretty-print output.
  8    from dual;

XMLELEMENT(PROPOSAL,XMLFORES
--------------------------------------------------------------------------------
<Proposal>
  <ProposalType>1</ProposalType>
  <CreateDate>01.11.2012</CreateDate>
  <Title>title1</Title>
  <InfoBlock>
    <Text>hello, world</Text>
    <Image id="100"/>
  </InfoBlock>
</Proposal>

由于您希望字符串是动态的,因此对该条目使用xmltype而不是xmlforest。只需确保“你好,世界”或其他任何正确编码。