我有一条看起来像这样的消息,但其中可能包含更多的发票:
<ns1:InvoicesEnvelope xmlns:ns1="http://Test.Schemas.InvoiceEnvelope_1"xmlns:ns0="http://Test.Schemas.Invoices">
<Invoices>
<ns0:Invoice>
<Header>
<BatchID>1311</BatchID>
<InvoiceNo>3400055151</InvoiceNo>
<CustomerName>CUSNAME1</CustomerName>
<TotalInvoiceLines>2</TotalInvoiceLines>
</Header>
<Lines>
<Line>
<TaxCode>S15</TaxCode>
<InvoiceAmt>12.77</InvoiceAmt>
</Line>
<Line>
<TaxCode>S15</TaxCode>
<InvoiceAmt>1.92</InvoiceAmt>
</Line>
</Lines>
</ns0:Invoice>
<ns0:Invoice>
<Header>
<BatchID>1311</BatchID>
<InvoiceNo>3400055152</InvoiceNo>
<CustomerName>CUSNAME2</CustomerName>
<TotalInvoiceLines>2</TotalInvoiceLines>
</Header>
<Lines>
<Line>
<TaxCode>S15</TaxCode>
<InvoiceAmt>12.77</InvoiceAmt>
</Line>
<Line>
<TaxCode>S15</TaxCode>
<InvoiceAmt>1.92</InvoiceAmt>
</Line>
</Lines>
</ns0:Invoice>
</Invoices>
</ns1:InvoicesEnvelope>
我想做的就是使用xpath
从原始邮件中获取第二张发票这是我的Xpath:
msgInvoice = xpath(msgInvoicesEnvelope, "string (//ns1:InvoicesEnvelope/Invoices/ns0:Invoice[position() = 2])”);
它返回的只是实际的字符串值连接在一起,如下所示:
13113400055152CUSNAME22S1512.77S151.92
我想要的是元素标签以及它可以放入新的单一发票消息中。这是我期望得到的:
<ns0:Invoice>
<Header>
<BatchID>1311</BatchID>
<InvoiceNo>3400055152</InvoiceNo>
<CustomerName>CUSNAME2</CustomerName>
<TotalInvoiceLines>2</TotalInvoiceLines>
</Header>
<Lines>
<Line>
<TaxCode>S15</TaxCode>
<InvoiceAmt>12.77</InvoiceAmt>
</Line>
<Line>
<TaxCode>S15</TaxCode>
<InvoiceAmt>1.92</InvoiceAmt>
</Line>
</Lines>
</ns0:Invoice>
</Invoices>
我做错了什么?
答案 0 :(得分:1)
我找到了问题的解决方案。这很简单。
它涉及正在使用的XPath表达式。
而不是说
msgInvoice = xpath(msgInvoicesEnvelope, "string (//ns1:InvoicesEnvelope/Invoices/ns0:Invoice[position() = 2])”);
省略字符串,返回值及其元素。
msgInvoice = xpath(msgInvoicesEnvelope, "//ns1:InvoicesEnvelope/Invoices/ns0:Invoice[position() = 2]”);
答案 1 :(得分:0)
你的XPath是对的;您的问题可能就是您要求XML数据;
例如,在C#中,如果我运行你的XPath并要求.InnerText属性,我会得到同样的虚假结果;但是,如果我把结果作为XmlElement
我可以正确处理它。
HTH