OpenXML无法使命名空间工作,可能是原始XML文件问题?

时间:2017-06-16 09:17:46

标签: sql-server xml

我在将工厂机器生成的XML文件导入SQL表时遇到问题。如果我编辑该文件以删除其他命名空间,我会得到正确的结果,但是因为我们想要自动化这个过程是不可行的。

XML文件的开头看起来像这样;

<?xml version="1.0" encoding="UTF-8"?>
<LaneHistory xmlns="omlNamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="omlNamespace C:\Program%20Files\Moba\Omnia%20MMI\MMI\bin\..\Application%20Data\XML\Schema\OML-LaneHistory.xsd">
<FileProperties>
    <MachineNumber>33309</MachineNumber>
    <CreationDateTime>2012-06-29T08:12:41</CreationDateTime>
    <ContentDescription>LaneHistory</ContentDescription>
    <Version>001</Version>
</FileProperties>
<Lane>
    <General>
        <StartDateTime>2012-06-29T05:50:02</StartDateTime>
        <StopDateTime>2012-06-29T06:20:07</StopDateTime>
        <LaneNumber>25</LaneNumber>
    </General>
    <Supply>
        <Property>
            <Name>Jones Heulyn Org Hse 3</Name>
            <AdditionalCode>UK20296=</AdditionalCode>
        </Property>
        <Identification>
            <TraceCode1>UK20296=</TraceCode1>
            <TraceCode2>633264</TraceCode2>

我尝试了以下内容;

DECLARE @XML AS XML, @hDoc AS INT, @SQL NVARCHAR (MAX), @rootxmlns varchar(MAX)
SELECT @XML = XMLData FROM XMLwithOpenXML
set @rootxmlns = '<LaneHistory xmlns="omlNamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="omlNamespace C:\Program%20Files\Moba\Omnia%20MMI\MMI\bin\..\Application%20Data\XML\Schema\OML-LaneHistory.xsd">/'

EXEC sp_xml_preparedocument @hDoc OUTPUT, @XML, @rootxmlns

但得到错误;

XML解析错误0xc00ce553发生在第0行,靠近XML文本“”。 消息6602,级别16,状态2,过程sp_xml_preparedocument,第1行[批次启动第33行] 错误描述是'以下标记未关闭:LaneHistory。'。

任何人都可以指出我正确的方向。

干杯 杰森

***其他

我的陈述的其余部分如下;

SELECT *
FROM OPENXML(@hDoc,'LaneHistory/Lane/Results/ResultPerGrade/WeightLimit')
WITH 
(
MachineNumber [varchar](7) '//FileProperties/MachineNumber',
StartDateTime [varchar](25) '../../../General/StartDateTime',
StopDateTime [varchar](125) '../../../General/StopDateTime',
LaneNumber [varchar](6) '../../../General/LaneNumber',
ExternalIdentification [varchar](30) '../../../Supply/ExternalIdentification/ID1',
FlkName [varchar](30) '../../../Supply/Property/Name',
FlkNumber [varchar](10) '../../../Supply/Identification/TraceCode2',
UKCode [varchar](10) '../../../Supply/Identification/TraceCode1',
ProductName [VarChar](30) '../../../Product/Property/Name',
PRoductType [VarChar](30) '../../../Product/Property/Type',
Package [VarChar](30) '../../../Product/Property/PackageName',
EggsPerLane [varchar](6) '../Count',
EggsGraded [FLOAT] '../../../Supply/Operation/AmountOfEggs',
EDescription [VarChar](30) '../Description',
EWeight [VarChar](30) '../Weight',
MinWeight [VarChar](30) '../WeightLimit/MinWeight',
MaxWeight [VarChar](30) '../WeightLimit/MaxWeight',
ExtraInfo1 [VarChar](20) '../../../Supply/Extra/Info1',
ExtraInfo2 [VarChar](20) '../../../Supply/Extra/Info2',
ExtraInfo3 [VarChar](20) '../../../Supply/Extra/Info3',
ExtraInfo4 [VarChar](20) '../../../Supply/Extra/Info4',
ExtraInfo5 [VarChar](20) '../../../Supply/Extra/Info5',
ShedNo [VarChar](6) '../../../Supply/Egg/ShedNumber',
ShedType [VarChar](30) '../../../Supply/Egg/Type',
LayDate [VarChar](12) '../../../Supply/Dates/LayDate',
SupplyDate [VarChar](12) '../../../Supply/Dates/SupplyDate',
BestBefore [VarChar](12) '../../../Product/Dates/BestBeforeDate'
)

EXEC sp_xml_removedocument @hDoc

2 个答案:

答案 0 :(得分:0)

在我看来,你的命名空间很糟糕:

set @rootxmlns = '<LaneHistory xmlns="omlNamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="omlNamespace C:\Program%20Files\Moba\Omnia%20MMI\MMI\bin\..\Application%20Data\XML\Schema\OML-LaneHistory.xsd">/'

你有&#34; /&#34;和&#34;&gt;&#34;最后是错误的顺序。我想你只需要这个:

set @rootxmlns = '<LaneHistory xmlns="omlNamespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="omlNamespace C:\Program%20Files\Moba\Omnia%20MMI\MMI\bin\..\Application%20Data\XML\Schema\OML-LaneHistory.xsd"/>'

MSDN有一个示例XML命名空间:

'<ROOT xmlns:xyz="urn:MyNamespace"/>'

......供参考。

第2部分 - 所以你的查询什么都不返回,我想这是因为当你使用OPENXML查询你的XML时,你设置了以属性为中心的选项(1)。如果将其更改为以元素为中心的选项(2),那么它应该有效吗?我使用您的部分XML进行了一些测试,可以通过将选项从1更改为2来弹出机器编号。

从基本的角度来说,这是以属性为中心的XML:

<LaneHistory><FileProperties MachineNumber="33309"/></LaneHistory>

...这是以元素为中心的XML;

<LaneHistory><FileProperties><MachineNumber>33309</MachineNumber></FileProperties></LaneHistory>

因此,要清楚,您的OPENXML语句需要第三个参数,该参数应为2。

答案 1 :(得分:0)

我终于修复了我必须将代码更改为以下内容的问题;

fabric.Image.prototype._render = function(ctx, noTransform) {
    /* This is the default behavior. I haven't modified anything in this part. */
    let x, y, imageMargins = this._findMargins(), elementToDraw;

    x = (noTransform ? this.left : -this.width / 2);
    y = (noTransform ? this.top : -this.height / 2);

    if (this.meetOrSlice === 'slice') {
        ctx.beginPath();
        ctx.rect(x, y, this.width, this.height);
        ctx.clip();
    }

    if (this.isMoving === false && this.resizeFilters.length && this._needsResize()) {
        this._lastScaleX = this.scaleX;
        this._lastScaleY = this.scaleY;
        elementToDraw = this.applyFilters(null, this.resizeFilters, this._filteredEl
            || this._originalElement, true);
    }
    else {
        elementToDraw = this._element;
    }

    /* My changes begin here. */
    if (elementToDraw && elementToDraw.naturalHeight > 0) {
        if (this.sizeMode === BadgingImageSizeMode.CenterImage) {
            drawCenterImage.apply(this, [ctx, elementToDraw, imageMargins, x, y]);
        } else {
            // Default _render behavior
            ctx.drawImage(elementToDraw,
                x + imageMargins.marginX,
                y + imageMargins.marginY,
                imageMargins.width,
                imageMargins.height);
        }
    }

    /* And they finish here. */
    this._stroke(ctx);
    this._renderStroke(ctx);
};