我是编写XSL的新手。 我试图使用XSLT从XML文件中提取一些数据。
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>
我正在尝试使用以下XSLT显示每个InvoiceLine的LineNumber和Amount:
<?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什么都不返回。
有什么问题?
非常感谢提前。
尼古拉斯
答案 0 :(得分: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如何将空前缀视为 null 命名空间:
XPath将空前缀视为null命名空间。换句话说,只有映射到名称空间的前缀才能用于XPath查询。这意味着如果要查询XML文档中的命名空间,即使它是默认命名空间,也需要为其定义前缀。 例如,如果没有为上面的XML文档定义前缀,XPath查询/书籍/书籍将不会返回任何结果。 必须绑定前缀以防止在查询某些节点不在命名空间中的文档时出现歧义,并且某些节点在默认命名空间中。