2

这是我的 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。感谢任何帮助。谢谢你。

4

1 回答 1

3

您的选择表达式有两个问题:

  1. 您没有在路径中使用名称空间。
  2. 你没有找到你想要的节点

命名空间问题在这里经常出现。XPath 选择器针对具有非空名称空间 URI 的节点的每个步骤都必须加上前缀(如果您使用名称选择器来定位它)。

如果你改变表达

<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>

是必需的//,因为您要定位的元素不在根目录中。 //通常避免使用,因为它“昂贵”;您如何准确定位该节点取决于您的要求。

于 2013-08-22T14:38:29.977 回答