如何从xml中检索元素值有多个来自SQL Server的命名空间?

时间:2016-07-02 04:54:19

标签: sql-server xml sql-server-2012

我试图从下面的XML文件中检索hasFolder属性值,并且无法处理我编写的查询。你能帮我找到正确的结果集吗?

我写的查询无效:

;with xmlnamespaces('http://www.omg.org/XMI' as xmi,
                    'rochade:scanboxi:150' as rim,
                    'http://www.asg.com/rochade/xml/xmiExtension/1.0' as rxe) 
select 
    data,
    data.value('(/xmi:XMI/rim:Repository/rim:name)[1]', 'varchar(max)')
from 
    Testxml

XML数据:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/XMI" xmi:version="2.0" 
         xmlns:rim="rochade:scanboxi:150" 
         xmlns:rxe="http://www.asg.com/rochade/xml/xmiExtension/1.0"  >
    <xmi:Documentation>
        <xmi:exporter>Rochade SAX Import/Exporter</xmi:exporter>
        <xmi:exporterVersion>2.10.xx</xmi:exporterVersion>
        <xmi:Extension extender="ASG-Rochade XML Facilities">
            <rxe:importModes>
                <mode category="ATTRIBUTE">KEEP</mode>
                <mode category="ATTRIBUTE.*.BOXI_HAS_INSTANCE">REMOVE</mode>
                <mode category="ATTRIBUTE.BOXI_RESULT_OBJECT.BOXI_SOURCED_FROM_COLUMN">REMOVE</mode>
                <mode category="ATTRIBUTE.BOXI_CRYSTAL_COLUMN.BOXI_CR_SOURCE_COLUMN_REF">REMOVE</mode>
                <mode category="ATTRIBUTE.BOXI_CRYSTAL_TABLE.BOXI_CR_SOURCE_TABLE_REF">REMOVE</mode>
                <mode category="ATTRIBUTE.BOXI_WEBI_REPORT.BOXI_HAS_PROMPT">REMOVE</mode>
                <mode category="ATTRIBUTE.BOXI_REPORT.BOXI_HAS_REPORT_ELEMENT">REMOVE</mode>
            </rxe:importModes>
        </xmi:Extension>
    </xmi:Documentation>
    <rim:Repository  xmi:id="_1"  rim:name="@BOBJPROD" >
        <rim:hasFolder  xmi:type="rim:Folder"  xmi:id="_3550"  rim:name="/FOLDERS/AdHoc/ARIOT AdHoc/Finance - Arizona/DSS"  rim:shortName="DSS"  rim:cmsScanTime="20150911 - 17:00:46"  rim:cmsScanUser="TU01822"  rim:cmsCreTime="20120802 - 16:25:38"  rim:cmsModTime="20120802 - 16:25:38"  rim:sID="28341084"  rim:cUID="AdcZXEQ8xUFLgEtZyS9IW0Y"  rim:pCUID="AWtaU4UM3iRBkI_lYBnXbns" >
            <rim:desc></rim:desc>
            <rim:hasBoObj  xmi:type="rim:Webi"  xmi:id="_17218"  rim:name="RRIS - Modifier Report"  rim:owner="JXM10"  rim:author="JXM10"  rim:cmsCreTime="20120822 - 10:38:24"  rim:cmsModTime="20121228 - 12:03:46"  rim:cmsScanTime="20150911 - 17:00:46"  rim:cmsScanUser="TU01822"  rim:sID="28915038"  rim:cUID="ASQ.gXvnAt9FqI7.AH7Gg2k"  rim:pCUID="AdcZXEQ8xUFLgEtZyS9IW0Y"  rim:mustFillContexts="false"  rim:mustFillPassword="false"  rim:mustFillPrompts="false"  rim:docLocaleType="LOCALE"  rim:formatOption="Webi"  rim:repSize="170,616" >
            <rim:desc>Total Gross Revenue Report</rim:desc>
            <rim:scanStat>ScanState OK </rim:scanStat>
            <rim:scanMessages>ScanRc 0
            ScanTime 20150911 - 17:00:46
            NO ScanMessages.</rim:scanMessages>
            <rim:keywords>Stats, Patient Visits, Gross Revenue</rim:keywords>
            <rim:bindStat>UNBOUND</rim:bindStat>
            <rim:hasReportField  xmi:type="rim:ReportField"  xmi:id="_860105"  rim:name="Count"  rim:fldType="VariableExpression"  rim:qualification="MEASURE"  rim:formulaLangId="[Count]" >
            <rim:formula>=1</rim:formula>
            </rim:hasReportField>
            <rim:hasReportField  xmi:type="rim:ReportField"  xmi:id="_860058"  rim:name="DP0.CPT Code"  rim:fldType="DPExpression"  rim:qualification="DIMENSION"  rim:formulaLangId="[CPT Code]"  rim:unvKey="CPT.CPT Code" >
            <rim:calcRef  xmi:type="rim:ResultObj"  xmi:idref="_860223"  />
            </rim:hasReportField>
            <rim:hasReportField  xmi:type="rim:ReportField"  xmi:id="_860077"  rim:name="DP0.CPT Description"  rim:fldType="DPExpression"  rim:qualification="DIMENSION"  rim:formulaLangId="[CPT Description]"  rim:unvKey="CPT.CPT Description" >
            <rim:calcRef  xmi:type="rim:ResultObj"  xmi:idref="_860224"  />
            </rim:hasReportField>

1 个答案:

答案 0 :(得分:1)

你的命名空间都设置得很好 - 这不是问题所在。但是你要混合属性和元素。该问题询问hasFolder属性 - 但这确实是一个带有大量属性的元素 - 您展示的代码实际上是在尝试阅读name 属性但正在解决它,好像它是一个元素。

如果您想从name=...元素中检索<rim:Repository> 属性,则需要使用此XQuery:

;with xmlnamespaces('http://www.omg.org/XMI' as xmi,
                    'rochade:scanboxi:150' as rim,
                    'http://www.asg.com/rochade/xml/xmiExtension/1.0' as rxe) 
select 
    data.value('(/xmi:XMI/rim:Repository/@rim:name)[1]', 'varchar(max)')
from 
    Testxml

您需要在其名称的开头添加@来表明您对属性感兴趣。

如果要将整个<rim:hasFolder> 元素检索为XML片段,则需要使用此XQuery:

;with xmlnamespaces('http://www.omg.org/XMI' as xmi,
                    'rochade:scanboxi:150' as rim,
                    'http://www.asg.com/rochade/xml/xmiExtension/1.0' as rxe) 
select 
    data.query('(/xmi:XMI/rim:Repository/rim:hasFolder)[1]')
from 
    Testxml

更新:如果您想从<rim:hasFolder>元素中检索单个属性,请使用以下语法:

如果要将整个<rim:hasFolder> 元素检索为XML片段,则需要使用此XQuery:

;with xmlnamespaces('http://www.omg.org/XMI' as xmi,
                    'rochade:scanboxi:150' as rim,
                    'http://www.asg.com/rochade/xml/xmiExtension/1.0' as rxe) 
select 
    data.value('(/xmi:XMI/rim:Repository/rim:hasFolder/@rim:name)[1]', 'varchar(200)')
from 
    Testxml

并根据需要调整其他属性