这是我的XSL转换文件:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:Message="http://schemas.microsoft.com/dynamics/2011/01/documents/Message"
xmlns:BankPositivePay="http://schemas.microsoft.com/dynamics/2008/01/documents/BankPositivePay">
<xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="no"/>
<xsl:template match="/">
<Header>
<records>
<record>
<xsl:value-of select="/BankPositivePay/BankAccountTable/AccountNum"/>
</record>
</records>
</Header>
</xsl:template>
</xsl:stylesheet>
输入的xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<Header>
<MessageId>{A604C46E-F3E3-4BCB-9F7A-E8FD8749A7FC}</MessageId>
<Action>http://tempuri.org/BankPositivePayService/find</Action>
</Header>
<Body>
<MessageParts xmlns="http://schemas.microsoft.com/dynamics/2011/01/documents/Message">
<BankPositivePay xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/BankPositivePay">
<BankAccountTable class="entity">
<AccountID>USA OPER</AccountID>
<AccountNum>34567</AccountNum>
<CurrencyCode>USD</CurrencyCode>
<LedgerDimension>
<MainAccount xmlns="http://schemas.microsoft.com/dynamics/2008/01/sharedtypes">110110</MainAccount>
</LedgerDimension>
<BankChequeTable class="entity">
<AccountID>USA OPER</AccountID>
<AmountCur>3500.00</AmountCur>
<ChequeNum>1132</ChequeNum>
<ChequeStatus>Payment</ChequeStatus>
<RecipientAccountNum>1001</RecipientAccountNum>
<TransDate>2013-08-16</TransDate>
<VendTable class="entity">
<Currency>USD</Currency>
<DefaultDimension>
<Values xmlns="http://schemas.microsoft.com/dynamics/2008/01/sharedtypes">
<Value>
<Name>CustomDepartment</Name>
<Value>060</Value>
</Value>
</Values>
</DefaultDimension>
<VendGroup>10</VendGroup>
</VendTable>
<CompanyInfo xsi:type="AxdEntity_CompanyInfo_CompanyInfo" class="entity" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<DataArea>ceu</DataArea>
</CompanyInfo>
</BankChequeTable>
</BankAccountTable>
</BankPositivePay>
</MessageParts>
</Body>
</Envelope>
问题是我得到的输出为:
<?xml version="1.0" encoding="utf-8"?>
<Header xmlns:Message="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" xmlns:BankPositivePay="http://schemas.microsoft.com/dynamics/2008/01/documents/BankPositivePay">
<records>
<record></record>
</records>
</Header>
记录元素为空,但它应该返回AccountNum 34567.任何帮助都表示赞赏。谢谢。
答案 0 :(得分:3)
您的选择表达式有两个问题:
命名空间问题经常出现在这里。针对具有非空名称空间URI 的节点的XPath选择器的每一步都必须加上前缀(如果您使用名称选择器将其作为目标)。
如果更改表达式
<xsl:value-of select="/BankPositivePay/BankAccountTable/AccountNum"/>
到
<xsl:value-of select="//BankPositivePay:BankPositivePay/BankPositivePay:BankAccountTable/BankPositivePay:AccountNum"/>
然后你的输出将是
<Header xmlns:Message="http://schemas.microsoft.com/dynamics/2011/01/documents/Message" xmlns:BankPositivePay="http://schemas.microsoft.com/dynamics/2008/01/documents/BankPositivePay">
<records>
<record>34567</record>
</records>
</Header>
需要//
,因为您定位的元素不在根目录中。 //
通常被避免,因为它“昂贵”;你如何准确定位该节点取决于你的要求。