我是XML新手,尤其是名称空间。我制作了所有文件,一切似乎都运行正常,但我不知道我是否真的使用名称空间(这是要求)。除了我的html文件无效,因为这个: “属性xmlns:这里不允许使用xsi。”和“属性xmlns:xslformatting不允许在这里。”
问题: 1.我真的使用名称空间吗? (如果不是我应该如何达到这个目标?) 2.如何使XSL生成有效的HTML?
<!DOCTYPE HTML>
<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xslFormatting="urn:xslFormatting">
XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="invoice.xsl" type="text/xsl"?>
<invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://josefdraslar.cz/ schema.xsd" xmlns="http://josefdraslar.cz/">
<buyerParty>
<name>Martin</name>
<surname>Vomáčka</surname>
<address1>
<street>Melantrichova</street>
<number>470/8</number>
<city>Praha</city>
<postalZone>11000</postalZone>
</address1>
<contact>
<fastContact>
<telephone>327345678</telephone>
<mobile>608473748</mobile>
<email>vomacka@gmail.com</email>
<fax/>
</fastContact>
<address>
<street>Melantrichova</street>
<number>470/8</number>
<city>Praha</city>
<postalZone>11000</postalZone>
<notes>čtvrté dveře vlevo</notes>
</address>
</contact>
<ico/>
</buyerParty>
<sellerParty>
<name>Alza.cz a.s.</name>
<address>
<street>Jateční</street>
<number>33a</number>
<city>Praha</city>
<postalZone>17000</postalZone>
<notes/>
</address>
<contact>
<telephone>327456709</telephone>
<mobile>607359724</mobile>
<email>info@alza.cz</email>
<fax>225340190</fax>
</contact>
<ico>27082440</ico>
<bankAccount>
<number>2171532</number>
<bank>
<kode>0800</kode>
<name>Česká spořitelna</name>
</bank>
<specificSymbol>345723</specificSymbol>
</bankAccount>
</sellerParty>
<invoiceInfo>
<issueDate>01.03.2015</issueDate>
<maturity>30.03.2015</maturity>
<invoiceNumber>345723</invoiceNumber>
<note/>
<numberOfItems>9</numberOfItems>
</invoiceInfo>
<sum>
<withoutTax>90000</withoutTax>
<tax>18900</tax>
<containingTaxTotal>108900</containingTaxTotal>
</sum>
<invoiceLineItems>
<invoiceItem>
<itemNumber>1234</itemNumber>
<itemName>Nokia SA</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1234</itemNumber>
<itemName>Nokia S</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1235</itemNumber>
<itemName>Nokia SB</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1236</itemNumber>
<itemName>Nokia SC</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1231</itemNumber>
<itemName>Nokia SR</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1239</itemNumber>
<itemName>Nokia SP</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1277</itemNumber>
<itemName>Nokia SX</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1222</itemNumber>
<itemName>Nokia SZ</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
<invoiceItem>
<itemNumber>1111</itemNumber>
<itemName>Nokia SI</itemName>
<numberOfUnits>1</numberOfUnits>
<price>
<perUnit>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</perUnit>
<sum>
<withoutTax>10000</withoutTax>
<containingTax>12100</containingTax>
</sum>
</price>
</invoiceItem>
</invoiceLineItems>
</invoice>
XSL(到HTML)
<?xml version="1.0" encoding="UTF-8"?>
<ins:stylesheet version="2.0"
xmlns:ins="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://josefdraslar.cz/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xslFormatting="urn:xslFormatting">
<ins:output method="html" version="5.0" encoding="UTF-8" indent="yes" />
<ins:template match="invoice">
<ins:result-document href="invoice.html">
<html>
<head>
<title> <ins:value-of select="sellerParty/name/text()"/></title>
<link rel="stylesheet" type="text/css" href="invoice.css"/>
</head>
<body>
<div class="hlavni">
<h1>Faktura</h1>
<ins:apply-templates/>
</div>
</body>
</html>
</ins:result-document>
</ins:template>
<ins:template match="buyerParty">
<h2>Kupující:</h2>
<p class="kupujici"><b>Jméno: </b><ins:value-of select="name/text()"/><ins:text> </ins:text>
<ins:value-of select="surname/text()"/><br/>
<b>Adresa: </b><ins:value-of select="/address1/street/text()"/>
<ins:text> </ins:text>
<ins:value-of select="address1/number/text()"/>
<ins:text>, </ins:text>
<ins:value-of select="address1/postalZone/text()"/>
<ins:text>, </ins:text>
<ins:value-of select="address1/city/text()"/> <br/>
<b>Kontaktní údaje</b><br/>
<b>telefon: </b> <ins:value-of select="contact/fastContact/telephone/text()"/><br/>
<b>email: </b> <ins:value-of select="contact/fastContact/email/text()"/> <br/>
</p>
</ins:template>
<ins:template match="sellerParty">
<h2>Prodávající:</h2>
<p class="prodavajici"><b>Jméno: </b><ins:value-of select="name/text()"/><br/>
<b>Adresa: </b><ins:value-of select="address/street/text()"/>
<ins:text> </ins:text>
<ins:value-of select="address/number/text()"/>
<ins:text>, </ins:text>
<ins:value-of select="address/postalZone/text()"/>
<ins:text>, </ins:text>
<ins:value-of select="address/city/text()"/> <br/>
<b>Kontaktní údaje</b><br/>
<b>telefon: </b> <ins:value-of select="contact/telephone/text()"/><br/>
<b>email: </b> <ins:value-of select="contact/email/text()"/> <br/>
</p>
</ins:template>
<ins:template match="invoiceInfo">
<h2>Obecné údaje:</h2>
<p class="faktura"><b>Datum vydání: </b><ins:value-of select="issueDate/text()"/><br/>
<b>Datum splatnosti: </b><ins:value-of select="maturity/text()"/> <br/>
<b>Identifikační číslo: </b> <ins:value-of select="invoiceNumber/text()"/><br/>
</p>
</ins:template>
<ins:template match="sum">
<h2>Hodnota:</h2>
<p class="faktura">
<b>Celková cena (bez DPH): </b> <ins:value-of select="withoutTax/text()"/><br/>
<b>Celkové DPH: </b> <ins:value-of select="tax/text()"/><br/>
<b>Celková cena (s DPH): </b> <ins:value-of select="containingTaxTotal/text()"/><br/>
</p>
</ins:template>
<ins:template match="invoiceLineItems">
<h3>Zboží:</h3>
<table>
<tr>
<td>Název</td>
<td>Počet kusů</td>
<td>Cena (s DPH)</td>
</tr>
<ins:for-each select="invoiceItem">
<ins:sort select="itemName"/>
<ins:call-template name="akce"/>
<tr>
<td><a href="{position()}.html"><ins:value-of select="./itemName"/></a></td>
<ins:choose>
<ins:when test="itemNumber > 1235">
<td style="color: red;"><ins:value-of select="./itemNumber"/></td>
</ins:when>
<ins:otherwise>
<td><ins:value-of select="./itemNumber"/></td>
</ins:otherwise>
</ins:choose>
<td><ins:value-of select="./price/sum/containingTax"/></td>
</tr>
</ins:for-each>
<tr>
<td></td>
<td>CELKEM</td>
<td><ins:value-of select="../sum/containingTaxTotal/text()"/></td>
</tr>
</table>
</ins:template>
<ins:template name="akce">
<ins:result-document href="{position()}.html">
<html>
<head>
<title>Zboží číslo <ins:value-of select="position()"/></title>
<link rel="stylesheet" type="text/css" href="invoice.css"/>
</head>
<body>
<div class="hlavni">
<table>
<tr>
<td>Název</td>
<td>Cena (bez DPH)</td>
<td>Cena (s DPH)</td>
</tr>
<tr>
<td><ins:value-of select="./itemName"/></td>
<td><ins:value-of select="./price/perUnit/withoutTax"/></td>
<td><ins:value-of select="./price/perUnit/containingTax"/></td>
</tr>
</table>
<p>
<a href="invoice.html"><ins:text>Zpět na hlavní stranu</ins:text></a>
</p>
</div>
</body>
</html>
</ins:result-document>
</ins:template>
</ins:stylesheet>
答案 0 :(得分:0)
要生成有效的输出HTML文档,您只需在样式表的exclude-result-prefixes="xsi xslFormatting"
(根)元素上添加<ins:stylesheet>
。
但实际上在您的样式表中,您无法在任何地方使用 xsi 和 xslFormatting 命名空间。因此,您还可以通过删除这些名称空间声明来修改样式表,从而导致:
<ins:stylesheet version="2.0"
xmlns:ins="http://www.w3.org/1999/XSL/Transform" xpath-default-namespace="http://josefdraslar.cz/">
唯一需要的命名空间绑定到&#34; http://josefdraslar.cz/&#34; URI,因为它是输入XML的默认命名空间,但是因为您声明了xpath-default-namespace="http://josefdraslar.cz/"
,所以在XSLT中正确使用了这个命名空间。