为什么我的 EXCEL 无法转换为 XML 文档,我该如何验证它?
Why is my XSL unable to be trasnformed into an XML doc and how do I get it to validate?
我整个周末和周一都在处理我的第一个 XSL 文件,但它不仅有问题,不允许我将它转换为基于 DTD 的 XML 文档,而且它还有我似乎无法弄清楚的错误。我设法解决了一些明显的问题,但作业要求我做的是:
将 "customers" 元素添加到根模板,应用订单模板并添加一个 id 属性节点,使用 custid 作为值...然后创建订单元素,订单 id 属性取自源,然后添加其他元素和属性以及从源中获取的值。也许我遇到了很多问题,因为我阅读的所有示例都使用了 XSL 文档中的 HTML 标签,但是这个必须是 XML 格式。
这是我的代码和来源:
<!ELEMENT customers (customer)*>
<!ELEMENT customer (order)>
<!ATTLIST customer id CDATA #IMPLIED>
<!ELEMENT order (qty, date, amount)>
<!ATTLIST order orderid CDATA #IMPLIED>
<!ELEMENT qty (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT amount (#PCDATA)>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
doctype-system="customers.dtd"
encoding="ISO-8859-1"
indent="yes" />
<xsl:template match="/">
<xsl:comment>
Author: Paulina Crawford
Date: 10/10/2015
</xsl:comment>
<xsl:element name="customers">
<xsl:apply-templates
select="orders/order">
<xsl:sort select="custid" />
</xsl:element>
</xsl:template>
<xsl:apply-templates select="order">
<xsl:element name="customer">
<xsl:attribute name="id">
<xsl:value-of select="@custid" />
<xsl:element name="order">
<xsl:attribute name="orderid">
<xsl:value-of select="@id" />
<xsl:element name="qty">
<xsl:value-of select="@qty" />
<xsl:element name="date">
<xsl:value-of select="date" />
<xsl:element name="amount">
<xsl:value-of select="amount" />
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="clist.xsl" ?>
<orders>
<order id="OR3124" qty="1" custid="CUST204">
<date>5/1/2017</date>
<amount>8.24</amount>
</order>
<order id="OR3125" qty="2" custid="CUST117">
<date>5/1/2017</date>
<amount>.21</amount>
</order>
<order id="OR3126" qty="1" custid="CUST311">
<date>5/1/2017</date>
<amount>.93</amount>
</order>
<order id="OR3127" qty="4" custid="CUST091">
<date>5/2/2017</date>
<amount>.21</amount>
</order>
<order id="OR3128" qty="1" custid="CUST137">
<date>5/2/2017</date>
<amount>7.24</amount>
</order>
<order id="OR3129" qty="1" custid="CUST128">
<date>5/3/2017</date>
<amount>.68</amount>
</order>
<order id="OR3130" qty="2" custid="CUST083">
<date>5/3/2017</date>
<amount>.93</amount>
</order>
<order id="OR3131" qty="1" custid="CUST304">
<date>5/3/2017</date>
<amount>2.25</amount>
</order>
</orders>
我收到的错误一直说我必须 "terminate the or element tag" 因为它是重复的或其他东西...如果有人可以提供帮助,我们将不胜感激...谢谢。
XSLT 必须格式正确 XML,这意味着对于每个开始标记,您都必须有一个结束标记,但您遗漏了很多。例如,当您创建一个 qty
元素时,您可以这样做...
<xsl:element name="qty">
<xsl:value-of select="@qty" />
但是没有结束 </xsl:element>
标签可见。如果您尝试缩进 XSLT,关闭标记与开始标记对齐,那么它会更明显。
您的 XSLT 应该看起来像这样
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" doctype-system="customers.dtd" encoding="ISO-8859-1" indent="yes" />
<xsl:template match="/">
<xsl:comment>
Author: Paulina Crawford
Date: 10/10/2015
</xsl:comment>
<xsl:element name="customers">
<xsl:apply-templates select="orders/order">
<xsl:sort select="custid" />
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="order">
<xsl:element name="customer">
<xsl:attribute name="id">
<xsl:value-of select="@custid" />
</xsl:attribute>
<xsl:element name="order">
<xsl:attribute name="orderid">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:element name="qty">
<xsl:value-of select="@qty" />
</xsl:element>
<xsl:element name="date">
<xsl:value-of select="date" />
</xsl:element>
<xsl:element name="amount">
<xsl:value-of select="amount" />
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
请注意,您实际上并不需要 xsl:element
来创建元素名称。直接写出你想输出的元素即可。也试试这个
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" doctype-system="customers.dtd" encoding="ISO-8859-1" indent="yes" />
<xsl:template match="/">
<xsl:comment>
Author: Paulina Crawford
Date: 10/10/2015
</xsl:comment>
<customers>
<xsl:apply-templates select="orders/order">
<xsl:sort select="custid" />
</xsl:apply-templates>
</customers>
</xsl:template>
<xsl:template match="order">
<customer id="{@custid}">
<order orderid="{@id}">
<qty>
<xsl:value-of select="@qty" />
</qty>
<date>
<xsl:value-of select="date" />
</date>
<amount>
<xsl:value-of select="amount" />
</amount>
</order>
</customer>
</xsl:template>
</xsl:stylesheet>
请注意 Attribute Value Templates 在创建某些属性时的使用。花括号表示要计算的表达式,而不是字面输出。
我整个周末和周一都在处理我的第一个 XSL 文件,但它不仅有问题,不允许我将它转换为基于 DTD 的 XML 文档,而且它还有我似乎无法弄清楚的错误。我设法解决了一些明显的问题,但作业要求我做的是:
将 "customers" 元素添加到根模板,应用订单模板并添加一个 id 属性节点,使用 custid 作为值...然后创建订单元素,订单 id 属性取自源,然后添加其他元素和属性以及从源中获取的值。也许我遇到了很多问题,因为我阅读的所有示例都使用了 XSL 文档中的 HTML 标签,但是这个必须是 XML 格式。
这是我的代码和来源:
<!ELEMENT customers (customer)*>
<!ELEMENT customer (order)>
<!ATTLIST customer id CDATA #IMPLIED>
<!ELEMENT order (qty, date, amount)>
<!ATTLIST order orderid CDATA #IMPLIED>
<!ELEMENT qty (#PCDATA)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT amount (#PCDATA)>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"
doctype-system="customers.dtd"
encoding="ISO-8859-1"
indent="yes" />
<xsl:template match="/">
<xsl:comment>
Author: Paulina Crawford
Date: 10/10/2015
</xsl:comment>
<xsl:element name="customers">
<xsl:apply-templates
select="orders/order">
<xsl:sort select="custid" />
</xsl:element>
</xsl:template>
<xsl:apply-templates select="order">
<xsl:element name="customer">
<xsl:attribute name="id">
<xsl:value-of select="@custid" />
<xsl:element name="order">
<xsl:attribute name="orderid">
<xsl:value-of select="@id" />
<xsl:element name="qty">
<xsl:value-of select="@qty" />
<xsl:element name="date">
<xsl:value-of select="date" />
<xsl:element name="amount">
<xsl:value-of select="amount" />
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="clist.xsl" ?>
<orders>
<order id="OR3124" qty="1" custid="CUST204">
<date>5/1/2017</date>
<amount>8.24</amount>
</order>
<order id="OR3125" qty="2" custid="CUST117">
<date>5/1/2017</date>
<amount>.21</amount>
</order>
<order id="OR3126" qty="1" custid="CUST311">
<date>5/1/2017</date>
<amount>.93</amount>
</order>
<order id="OR3127" qty="4" custid="CUST091">
<date>5/2/2017</date>
<amount>.21</amount>
</order>
<order id="OR3128" qty="1" custid="CUST137">
<date>5/2/2017</date>
<amount>7.24</amount>
</order>
<order id="OR3129" qty="1" custid="CUST128">
<date>5/3/2017</date>
<amount>.68</amount>
</order>
<order id="OR3130" qty="2" custid="CUST083">
<date>5/3/2017</date>
<amount>.93</amount>
</order>
<order id="OR3131" qty="1" custid="CUST304">
<date>5/3/2017</date>
<amount>2.25</amount>
</order>
</orders>
我收到的错误一直说我必须 "terminate the or element tag" 因为它是重复的或其他东西...如果有人可以提供帮助,我们将不胜感激...谢谢。
XSLT 必须格式正确 XML,这意味着对于每个开始标记,您都必须有一个结束标记,但您遗漏了很多。例如,当您创建一个 qty
元素时,您可以这样做...
<xsl:element name="qty">
<xsl:value-of select="@qty" />
但是没有结束 </xsl:element>
标签可见。如果您尝试缩进 XSLT,关闭标记与开始标记对齐,那么它会更明显。
您的 XSLT 应该看起来像这样
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" doctype-system="customers.dtd" encoding="ISO-8859-1" indent="yes" />
<xsl:template match="/">
<xsl:comment>
Author: Paulina Crawford
Date: 10/10/2015
</xsl:comment>
<xsl:element name="customers">
<xsl:apply-templates select="orders/order">
<xsl:sort select="custid" />
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="order">
<xsl:element name="customer">
<xsl:attribute name="id">
<xsl:value-of select="@custid" />
</xsl:attribute>
<xsl:element name="order">
<xsl:attribute name="orderid">
<xsl:value-of select="@id" />
</xsl:attribute>
<xsl:element name="qty">
<xsl:value-of select="@qty" />
</xsl:element>
<xsl:element name="date">
<xsl:value-of select="date" />
</xsl:element>
<xsl:element name="amount">
<xsl:value-of select="amount" />
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
请注意,您实际上并不需要 xsl:element
来创建元素名称。直接写出你想输出的元素即可。也试试这个
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" doctype-system="customers.dtd" encoding="ISO-8859-1" indent="yes" />
<xsl:template match="/">
<xsl:comment>
Author: Paulina Crawford
Date: 10/10/2015
</xsl:comment>
<customers>
<xsl:apply-templates select="orders/order">
<xsl:sort select="custid" />
</xsl:apply-templates>
</customers>
</xsl:template>
<xsl:template match="order">
<customer id="{@custid}">
<order orderid="{@id}">
<qty>
<xsl:value-of select="@qty" />
</qty>
<date>
<xsl:value-of select="date" />
</date>
<amount>
<xsl:value-of select="amount" />
</amount>
</order>
</customer>
</xsl:template>
</xsl:stylesheet>
请注意 Attribute Value Templates 在创建某些属性时的使用。花括号表示要计算的表达式,而不是字面输出。