0

我有一个这样的 XML 示例文件:

<?xml version="1.0" encoding="UTF-8"?>
<!--Sample XML file generated by XMLSpy v2011 rel. 2 (x64) (http://www.altova.com)-->
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.05"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:pain.001.001.05 file://BE-FILE01/tverschu$/SR%20(Standards%20Release)/SR2013/ISO/Payments%20Initiation/SR2013_MX_Schemas_PaymentsInitiation/pain.001.001.05.xsd">
<CstmrCdtTrfInitn>
    <GrpHdr>
        <MsgId>ABC/120928/CCT001</MsgId>
        <CreDtTm>2012-09-28T14:07:00</CreDtTm>
        <NbOfTxs>3</NbOfTxs>
        <CtrlSum>11500000</CtrlSum>
        <InitgPty>
            <Nm>ABC Corporation</Nm>
            <PstlAdr>
                <StrtNm>Times Square</StrtNm>
                <BldgNb>7</BldgNb>
                <PstCd>NY 10036</PstCd>
                <TwnNm>New York</TwnNm>
                <Ctry>US</Ctry>
            </PstlAdr>
        </InitgPty>
    </GrpHdr>
    <PmtInf>
        <PmtInfId>ABC/086</PmtInfId>
        <PmtMtd>TRF</PmtMtd>
        <BtchBookg>false</BtchBookg>
        <ReqdExctnDt>2012-09-29</ReqdExctnDt>
        <Dbtr>
            <Nm>ABC Corporation</Nm>
            <PstlAdr>
                <StrtNm>Times Square</StrtNm>
                <BldgNb>7</BldgNb>
                <PstCd>NY 10036</PstCd>
                <TwnNm>New York</TwnNm>
                <Ctry>US</Ctry>
            </PstlAdr>
        </Dbtr>
        <DbtrAcct>
            <Id>
                <Othr>
                    <Id>00125574999</Id>
                </Othr>
            </Id>
        </DbtrAcct>
        <DbtrAgt>
            <FinInstnId>
                <BICFI>BBBBUS33</BICFI>
            </FinInstnId>
        </DbtrAgt>
        <CdtTrfTxInf>
            <PmtId>
                <InstrId>ABC/120928/CCT001/01</InstrId>
                <EndToEndId>ABC/4562/2012-09-08</EndToEndId>
            </PmtId>
            <Amt>
                <InstdAmt Ccy="JPY">10000000</InstdAmt>
            </Amt>
            <ChrgBr>SHAR</ChrgBr>
            <CdtrAgt>
                <FinInstnId>
                    <BICFI>AAAAGB2L</BICFI>
                </FinInstnId>
            </CdtrAgt>
            <Cdtr>
                <Nm>DEF Electronics</Nm>
                <PstlAdr>
                    <AdrLine>Corn Exchange 5th Floor</AdrLine>
                    <AdrLine>Mark Lane 55</AdrLine>
                    <AdrLine>EC3R7NE London</AdrLine>
                    <AdrLine>GB</AdrLine>
                </PstlAdr>
            </Cdtr>
            <CdtrAcct>
                <Id>
                    <Othr>
                        <Id>23683707994125</Id>
                    </Othr>
                </Id>
            </CdtrAcct>
            <Purp>
                <Cd>GDDS</Cd>
            </Purp>
            <RmtInf>
                <Strd>
                    <RfrdDocInf>
                        <Tp>
                            <CdOrPrtry>
                                <Cd>CINV</Cd>
                            </CdOrPrtry>
                        </Tp>
                        <Nb>4562</Nb>
                        <RltdDt>2012-09-08</RltdDt>
                    </RfrdDocInf>
                </Strd>
            </RmtInf>
        </CdtTrfTxInf>
        <CdtTrfTxInf>
            <PmtId>
                <InstrId>ABC/120928/CCT001/2</InstrId>
                <EndToEndId>ABC/ABC-13679/2012-09-15</EndToEndId>
            </PmtId>
            <Amt>
                <InstdAmt Ccy="EUR">500000</InstdAmt>
            </Amt>
            <ChrgBr>CRED</ChrgBr>
            <CdtrAgt>
                <FinInstnId>
                    <BICFI>DDDDBEBB</BICFI>
                </FinInstnId>
            </CdtrAgt>
            <Cdtr>
                <Nm>GHI Semiconductors</Nm>
                <PstlAdr>
                    <StrtNm>Avenue Brugmann</StrtNm>
                    <BldgNb>415</BldgNb>
                    <PstCd>1180</PstCd>
                    <TwnNm>Brussels</TwnNm>
                    <Ctry>BE</Ctry>
                </PstlAdr>
            </Cdtr>
            <CdtrAcct>
                <Id>
                    <IBAN>BE30001216371411</IBAN>
                </Id>
            </CdtrAcct>
            <InstrForCdtrAgt>
                <Cd>PHOB</Cd>
                <InstrInf>+32/2/2222222</InstrInf>
            </InstrForCdtrAgt>
            <Purp>
                <Cd>GDDS</Cd>
            </Purp>
            <RmtInf>
                <Strd>
                    <RfrdDocInf>
                        <Tp>
                            <CdOrPrtry>
                                <Cd>CINV</Cd>
                            </CdOrPrtry>
                        </Tp>
                        <Nb>ABC-13679</Nb>
                        <RltdDt>2012-09-15</RltdDt>
                    </RfrdDocInf>
                </Strd>
            </RmtInf>
        </CdtTrfTxInf>
        <CdtTrfTxInf>
            <PmtId>
                <InstrId>ABC/120928/CCT001/3</InstrId>
                <EndToEndId>ABC/987-AC/2012-09-27</EndToEndId>
            </PmtId>
            <Amt>
                <InstdAmt Ccy="USD">1000000</InstdAmt>
            </Amt>
            <ChrgBr>SHAR</ChrgBr>
            <CdtrAgt>
                <FinInstnId>
                    <BICFI>BBBBUS66</BICFI>
                </FinInstnId>
            </CdtrAgt>
            <Cdtr>
                <Nm>ABC Corporation</Nm>
                <PstlAdr>
                    <Dept>Treasury department</Dept>
                    <StrtNm>Bush Street</StrtNm>
                    <BldgNb>13</BldgNb>
                    <PstCd>CA 94108</PstCd>
                    <TwnNm>San Francisco</TwnNm>
                    <Ctry>US</Ctry>
                </PstlAdr>
            </Cdtr>
            <CdtrAcct>
                <Id>
                    <Othr>
                        <Id>4895623</Id>
                    </Othr>
                </Id>
            </CdtrAcct>
            <Purp>
                <Cd>INTC</Cd>
            </Purp>
            <RmtInf>
                <Strd>
                    <RfrdDocInf>
                        <Tp>
                            <CdOrPrtry>
                                <Cd>CINV</Cd>
                            </CdOrPrtry>
                        </Tp>
                        <Nb>987-AC</Nb>
                        <RltdDt>2012-09-27</RltdDt>
                    </RfrdDocInf>
                </Strd>
            </RmtInf>
        </CdtTrfTxInf>
    </PmtInf>
</CstmrCdtTrfInitn>
</Document>

我想从 Document 标签中检索 xmlns 属性值。为此,我使用 xpath 的评估方法:

            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        InputStream inputStream = new FileInputStream(new File("Business sample 1 pain.001.xml"));
        org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);
        StringWriter stw = new StringWriter();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.transform(new DOMSource(doc), new StreamResult(stw));

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();


        String xml = stw.toString();

        InputSource xmlsource = new InputSource(new StringReader(xml));

        String value = xpath.evaluate(
            "Document/attribute::xmlns", xmlsource);

        System.out.println(value);

此代码将值返回为“”。

我试过改变:

String value = xpath.evaluate("Document/attribute::xmlns", xmlsource);

为了:

String value = xpath.evaluate("Document/CstmrCdtTrfInitn/GrpHdr/MsgId", xmlsource);

进行测试,我得到了相同的回报“”。

我做错了什么?

我需要的回报是pain.001.001.05,但如果我得到所有字符串urn:iso:std:iso:20022:tech:xsd:pain.001.001.05,它对我也很有用。

先感谢您。

4

3 回答 3

3

xmlns="..."声明意味着Document元素(及其所有后代)位于指定的命名空间中,因此您不会得到任何匹配,/Document因为它只会选择Document不在命名空间中的命名元素 - 并且您不能为正确的名称空间,因为“正确的名称空间”是您首先要查找的内容!

但是您不需要 XPath 来执行此操作 - 您已经拥有

org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream);

这是原始文档的非命名空间感知的DOM 树(因为DocumentBuilderFactory默认情况下不感知命名空间),其中xmlns 视为属性,因此您可以使用

String xmlnsVal = doc.getDocumentElement().getAttribute("xmlns");
String lastSegment = xmlnsVal.substring(xmlnsVal.lastIndexOf(':') + 1);
// this is safe even if xmlnsVal doesn't contain any colons - lastIndexOf
// will return -1, so substring(0) is the whole xmlnsVal

如果您在启用命名空间的情况下进行解析,即您之前调用documentBuilderFactory.setNamespaceAware(true)newDocumentBuilder(),那么您可以使用

String xmlnsVal = doc.getDocumentElement().getNamespaceURI();
于 2013-06-24T11:45:58.253 回答
1

我认为"namespace-uri(/*)"XPath 将返回 URI(完整的字符串,不仅在 last 之后:)。

于 2013-06-24T11:32:04.390 回答
0

您需要考虑默认命名空间声明,如如何在具有默认命名空间的 xml 文档上使用 XPath中所示。

于 2013-06-24T11:23:41.063 回答