2015-07-23 3 views
0

Я новичок в написании XSL. Я пытаюсь извлечь некоторые данные из файла XML с помощью XSLT.XSL ничего не возвращает

Файл XML и файл XSL приведены ниже.

Это мой XML-файл (полный не экстракт, чтобы быть уверенным ;-):

<?xml version="1.0" encoding="UTF-8"?> 
<AggregatedBillData xmlns="http://www.atrias.be/migdgo/20150630/Bill/AggregatedBillData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ent="http://www.atrias.be/migdgo/20150630/Entities" xsi:schemaLocation="http://www.atrias.be/migdgo/20150630/Bill/AggregatedBillData ../../Messages/Bill/DGO-BI-INT-50-AggregatedBillData.xsd"> 
<HeaderMessage> 
    <ent:MessageId>d83c111c-e655-4481-8022-fb2e0e88dd4a</ent:MessageId> 
    <ent:MessageDateTime>2018-07-11T12:00:00+02:00</ent:MessageDateTime> 
    <ent:ContextId>011c38c8-71f6-4730-bf44-2ca1f9d2f12e</ent:ContextId> 
    <ent:Module>I81</ent:Module> 
    <ent:Label>IA2</ent:Label> 
    <ent:Sector>23</ent:Sector> 
    <ent:SenderId>5414495999995</ent:SenderId> 
    <ent:ReceiverId>5414488009809</ent:ReceiverId> 
    <ent:MinorVersion>0</ent:MinorVersion> 
</HeaderMessage> 
<Invoice> 
    <Id>0</Id> 
    <Sector>23</Sector> 
    <BillType>IE6</BillType> 
    <BillPurpose>IE5</BillPurpose> 
    <RectificationFlag>false</RectificationFlag> 
    <CalculationDateTime>2018-01-01T12:00:00+01:00</CalculationDateTime> 
    <GridAccessProvider> 
     <ent:EanGln>5499780282808</ent:EanGln> 
    </GridAccessProvider> 
    <InvoiceLine> 
     <LineNumber>500004500000001</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--01</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000002</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--02</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000003</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--03</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000004</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--04</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000005</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--05</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000006</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--06</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000007</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--07</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000008</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--08</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000009</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--09</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000010</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--10</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000011</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--11</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <InvoiceLine> 
     <LineNumber>500004500000012</LineNumber> 
     <Amount>250</Amount> 
     <Currency>EUR</Currency> 
     <Quantity>1000</Quantity> 
     <Unit>KWH</Unit> 
     <ConsumptionMonth>--12</ConsumptionMonth> 
     <CreditLineFlag>false</CreditLineFlag> 
     <TariffComponent> 
      <Code>?</Code> 
      <Direction>E17</Direction> 
      <CostCategory>D</CostCategory> 
      <VatRate>0.21</VatRate> 
      <GlobalisationCode> 
       <Code>E</Code> 
      </GlobalisationCode> 
      <Tariff> 
       <Code>T220</Code> 
      </Tariff> 
     </TariffComponent> 
    </InvoiceLine> 
    <MarketParty> 
     <ent:EanGln>0000000000000</ent:EanGln> 
    </MarketParty> 
    <BillArea> 
     <Id>4444</Id> 
     <Sector>23</Sector> 
    </BillArea> 
</Invoice> 
<AggregationRun> 
    <ent:Id>1</ent:Id> 
    <ent:NumberOfAggregations>12</ent:NumberOfAggregations> 
    <ent:TotalAmount>1200</ent:TotalAmount> 
    <ent:Aggregation> 
     <ent:Id>1</ent:Id> 
     <ent:TotalAmount>100</ent:TotalAmount> 
    </ent:Aggregation> 
</AggregationRun> 

Я пытаюсь отобразить LINENUMBER и количество для каждого InvoiceLine со следующим XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
    <xsl:stylesheet version="1.0" mlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
    <xsl:template match="/"> 
     <html> 
      <body> 
       <h2>Aggregated Bill</h2> 
       <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th>LineNumber</th> 
         <th>Amount</th> 
        </tr> 
        <xsl:for-each select="AggregatedBillData/Invoice/InvoiceLine"> 
         <tr> 
          <td> 
           <xsl:value-of select="LineNumber"/> 
          </td> 
          <td> 
           <xsl:value-of select="Amount"/> 
          </td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
    </xsl:stylesheet> 

XSLT ничего не возвращает.

В чем проблема?

Большое спасибо заранее.

Nicolas

+2

Ваш XML-образец недопустим - вам не хватает закрывающего тега 'AggregatedBillData'? –

+4

Кроме того, ваш XSLT-вход недействителен, согласно Саксону. Строка 2 должна читать 'xmlns: xsl = ..' not 'mlns: xsl = ..'. Ваш XSLT-процессор должен был сказать вам об этом; если это не так, используйте другой. Пожалуйста, убедитесь, что ваши образцы не содержат ** тривиальных ** таких ошибок. – usr2564301

ответ

0

У вас есть пространство имен по умолчанию, объявленное в исходном XML: xmlns="http://www.atrias.be/migdgo/20150630/Bill/AggregatedBillData"

Вам нужно добавить эту декларацию (включая префикс пространства имен) в вашем XSLT и использовать его в XPath выражений:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:my="http://www.atrias.be/migdgo/20150630/Bill/AggregatedBillData"> 
    <xsl:template match="/"> 
     <html> 
      <body> 
       <h2>Aggregated Bill</h2> 
       <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th>LineNumber</th> 
         <th>Amount</th> 
        </tr> 
        <xsl:for-each select="my:AggregatedBillData/my:Invoice/my:InvoiceLine"> 
         <tr> 
          <td> 
           <xsl:value-of select="my:LineNumber"/> 
          </td> 
          <td> 
           <xsl:value-of select="my:Amount"/> 
          </td> 
         </tr> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
    </xsl:stylesheet> 

Вы должны указать префикс для ссылки на элементы из этого пространства имен в XPath.

Если посмотреть на this MSDN page это объясняет немного о том, как XPath обрабатывает пустой префикс в качестве пространства имен нуля:

XPath обрабатывает пустой префикс в качестве нулевого пространства имен. Другими словами, в запросах XPath можно использовать только префиксы, сопоставленные пространствам имен. Это означает, что если вы хотите запросить пространство имен в документе XML, даже если это пространство имен по умолчанию, вам нужно определить префикс для него. Например, без указания префикса для документа XML выше, запрос/книга/книга XPath не вернет никаких результатов. Префикс должен быть связан для предотвращения неоднозначности при запросе документов с некоторыми узлами, не содержащимися в пространстве имен, а также в пространстве имен по умолчанию.

+0

Большое спасибо. никола –

Смежные вопросы