1

我有一个表列,它存储 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>
4

2 回答 2

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"/>
于 2012-10-31T19:50:30.883 回答
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。只需确保“你好,世界”或任何正确编码的内容。

于 2012-11-01T16:00:33.803 回答