Parse XML - 检索双引号之间的部分

Parse XML - Retrieve the Portion Between the Double Quotes

我在 SQL 服务器的 XML 列中有以下 XML。我能够检索标签之间的数据,并使用底部的代码以 table 格式列出它。我可以检索所有标签之间的值,除了我在下面以粗体显示的那个用双引号引起来的值。我可以得到值 X 就好了,但我需要得到这部分双引号之间的 6:<Organization501cInd organization501cTypeTxt="6">X</Organization501cInd>

WITH XMLNAMESPACES (DEFAULT 'http://www.irs.gov/efile')
SELECT ID, FilingYear, FilingPeriod, FilingType, [FileName]

, Organization501c3Ind = c.value('(//Organization501c3Ind/text())[1]','varchar(MAX)')
, Organization501cInd = c.value('(//Organization501cInd/text())[1]','varchar(MAX)')
, Organization501cTypeTxt = c.value('(//Organization501cTypeTxt/text())[1]','varchar(MAX)')


FROM Form990
    CROSS APPLY XMLData.nodes('//Return') AS t(c)
    CROSS APPLY XMLData.nodes('//Return/ReturnHeader/Filer') AS t2(c2)

XML:

<ReturnData documentCnt="2">
    <IRS990 documentId="IRS990-01" referenceDocumentId="IRS990ScheduleO-01" referenceDocumentName="IRS990ScheduleO ReasonableCauseExplanation" softwareId="19009670">
      <PrincipalOfficerNm>CAREY BAKER</PrincipalOfficerNm>
      <USAddress>
        <AddressLine1Txt>PO BOX 11275</AddressLine1Txt>
        <CityNm>TALLAHASSEE</CityNm>
        <StateAbbreviationCd>FL</StateAbbreviationCd>
        <ZIPCd>32302</ZIPCd>
      </USAddress>
      <GrossReceiptsAmt>104241</GrossReceiptsAmt>
      <GroupReturnForAffiliatesInd>false</GroupReturnForAffiliatesInd>
      <Organization501cInd organization501cTypeTxt="6">X</Organization501cInd>

想法?

NOTE: XML element and attribute names are case-sensitive. i.e.: Organization501cTypeTxt will not match an attribute named organization501cTypeTxt.

提取属性时,您需要在 XPath 查询中使用 @ 访问器。试试下面的方法...

WITH XMLNAMESPACES (DEFAULT 'http://www.irs.gov/efile')
SELECT  ID, FilingYear, FilingPeriod, FilingType, [FileName],
    Organization501cInd = c2.value('(Organization501cInd/text())[1]','varchar(MAX)'),
    organization501cTypeTxt = c2.value('(Organization501cInd/@organization501cTypeTxt)[1]','varchar(MAX)')
FROM Form990
    CROSS APPLY XMLData.nodes('/ReturnData') AS t(c)
    CROSS APPLY t.c.nodes('IRS990') AS t2(c2);

没有 OP 的最小可重现示例,从臀部拍摄。

SQL

-- DDL and sample data population, start
DECLARE @Form990 TABLE (ID INT IDENTITY PRIMARY KEY, XMLData XML);
INSERT INTO @Form990(XMLData) VALUES
(N'<Return xmlns="http://www.irs.gov/efile" returnVersion="2019v5.1">
    <ReturnData documentCnt="2">
        <IRS990 documentId="IRS990-01" referenceDocumentId="IRS990ScheduleO-01" referenceDocumentName="IRS990ScheduleO ReasonableCauseExplanation" softwareId="19009670">
            <PrincipalOfficerNm>CAREY BAKER</PrincipalOfficerNm>
            <USAddress>
                <AddressLine1Txt>PO BOX 11275</AddressLine1Txt>
                <CityNm>TALLAHASSEE</CityNm>
                <StateAbbreviationCd>FL</StateAbbreviationCd>
                <ZIPCd>32302</ZIPCd>
            </USAddress>
            <GrossReceiptsAmt>104241</GrossReceiptsAmt>
            <GroupReturnForAffiliatesInd>false</GroupReturnForAffiliatesInd>
            <Organization501c3Ind>X</Organization501c3Ind>
            <Organization501cInd organization501cTypeTxt="6">X</Organization501cInd>
        </IRS990>
    </ReturnData>
</Return>');
-- DDL and sample data population, end

WITH XMLNAMESPACES (DEFAULT 'http://www.irs.gov/efile')
SELECT -- ID, FilingYear, FilingPeriod, FilingType, [FileName]
    Organization501c3Ind = c.value('(Organization501c3Ind/text())[1]','varchar(MAX)')
    , Organization501cInd = c.value('(Organization501cInd/text())[1]','varchar(MAX)')
    , Organization501cTypeTxt = c.value('(Organization501cInd/@organization501cTypeTxt)[1]','varchar(MAX)')
FROM @Form990
    CROSS APPLY XMLData.nodes('/Return/ReturnData/IRS990') AS t(c)

输出

+----------------------+---------------------+-------------------------+
| Organization501c3Ind | Organization501cInd | Organization501cTypeTxt |
+----------------------+---------------------+-------------------------+
| X                    | X                   |                       6 |
+----------------------+---------------------+-------------------------+