perl XML设置标签值问题

时间:2018-11-21 22:42:15

标签: xml perl

我有一些代码可以根据一个非常简单的字符串不同地创建XML。有人知道为什么吗?

首先,编写代码片段(以澄清@mob的问题),并在问题行周围加上以下行:

my $XML = XML::Smart->new();

my $mylogin = {
    userId => $SQLUser,
    password => $SQLPass,
    maxIdle => '900000',
    properties => '<hidden for privacy concerns>'
};
$XML->{'System-Link'}{Login} = $mylogin;

my $request = {
    sessionHandle => '*current',
    workHandle => '*new',
    broker => 'EJB',
    maxIdle => '900000'
};
$XML->{'System-Link'}{Request} = $request;

my $querylist = {
    name => 'queryListPurchaseOrderItemRelease_SLTOKEN',
    domainClass => '<hidden for privacy concerns>',
    includeMetaData => 'true',
    maxReturned => '1'
};
$XML->{'System-Link'}{Request}{QueryList} = $querylist;
### PROBLEM LINE GOES HERE, just subsitute one of below lines here

## Muck with the DTD cause 'EMPTY' causes problems, this is an issue w/ the implementation of XML::Smart
open $dtd, '<', 'SystemLinkRequest.dtd';
my $dtdContent = do { local $/; <$dtd> };
close $dtd;
$dtdContent =~ s/EMPTY//g;

$XML->apply_dtd($dtdContent);

## Turn off output of DTD and meta tag generation
my $xmldata = $XML->data(nometagen => 1, nodtd => 1);
$xmldata =~ s/\?>/?>\n<!DOCTYPE System-Link SYSTEM 'SystemLinkRequest.dtd'>/m;
## Store a clean version (no passwords) in the local XML file
my $xmldata_clean = $xmldata;
$xmldata_clean =~ s/userId\s*=\s*\"$SQLUser\"\s+password\s*=\s*\"$SQLPass\"/userId=\"REMOVED\" password=\"REMOVED\"/g;

my $xml_filename = $RELEASE . "-" . time . ".xml";
print LOGF "{DEBUG} [" . localtime(time) . "] \tBuilding XML Request COMPLETED\n";
print LOGF "{DEBUG} [" . localtime(time) . "] \tWriting XML Request to Request_$xml_filename STARTED\n";

open $RequestLogfile, '>', "$logsDir/XML/Update/Request_$xml_filename";
print $RequestLogfile "\n--------------- QUERY REQUEST ---------------\n";
print $RequestLogfile $xmldata;
close $RequestLogfile;

作品:

$XML->{'System-Link'}{Request}{QueryList}{Pql} = "SELECT order,line,item,release,dueToDockDate,dueToStockDate,userFieldAmount1 WHERE (releaseStatus < '50' AND userFieldAmount1 = '$RELEASE' AND relatedQueryPurchaseOrder.orderStatus < '40' AND remainingQuantityForReleaseStockingUm > 0.000) ORDER BY order,line,release";

不起作用:

$XML->{'System-Link'}{Request}{QueryList}{Pql} = "SELECT order,line,item,release,dueToDockDate,dueToStockDate,userFieldAmount1 WHERE (releaseStatus < '50' AND userFieldAmount1 = '$RELEASE' AND relatedQueryPurchaseOrder.orderStatus < '40') ORDER BY order,line,release";

现在我得到了XML。

好的XML(带有CDATA的Pql标记):

<QueryList domainClass="com.mapics.pm.PoItemRelease" includeMetaData="true" maxReturned="1" name="queryListPurchaseOrderItemRelease_SLTOKEN">
<Pql><![CDATA[SELECT order,line,item,release,dueToDockDate,dueToStockDate,userFieldAmount1 WHERE (releaseStatus < '50' AND userFieldAmount1 = '4001166' AND relatedQueryPurchaseOrder.orderStatus < '40' AND remainingQuantityForReleaseStockingUm > 0.000) ORDER BY order,line,release]]></Pql>
</QueryList>

错误的XML(Pql属性):

<QueryList Pql="SELECT order,line,item,release,dueToDockDate,dueToStockDate,userFieldAmount1 WHERE (releaseStatus &lt; '50' AND userFieldAmount1 = '4001166' AND relatedQueryPurchaseOrder.orderStatus &lt; '40' ) ORDER BY order,line,release" domainClass="com.mapics.pm.PoItemRelease" includeMetaData="true" maxReturned="1" name="queryListPurchaseOrderItemRelease_SLTOKEN"/>

这两行代码之间的唯一区别是,起作用的那一行具有AND remainingQuantityForReleaseStockingUm > 0.000

那为什么会影响生成的XML?

1 个答案:

答案 0 :(得分:0)

XML::Smart::_data_type包含以下代码:

  return 3 if( $data && $data =~ /<.*?>/s    ) ;

其中数据类型“ 3”指示XML元素应呈现为CDATA。如果那是“好的XML”,那只是偶然。

我想您可能会诱骗XML::Smart使用一个额外的子句将所有SQL查询呈现为CDATA

SELECT ... WHERE (... AND ... AND 1<>0) ORDER BY ...