我整个周末和周一一直在处理我的第一个XSL文件,但不仅是它有问题,不允许我将它转换为基于DTD的XML文档,但它也有我看不出的错误弄清楚。我设法解决了一些明显的问题,但所有的任务要求我做的是:
添加"客户"将元素添加到根模板,应用订单模板并添加id属性节点,使用custid作为值...然后使用从源获取的order id属性创建order元素,然后添加其他元素和属性以及采用的值来自消息来源。也许我遇到了这么多问题,因为我读过的所有例子都在XSL doc中使用了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)>
&#13;
<?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>
&#13;
<?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>$108.24</amount>
</order>
<order id="OR3125" qty="2" custid="CUST117">
<date>5/1/2017</date>
<amount>$78.21</amount>
</order>
<order id="OR3126" qty="1" custid="CUST311">
<date>5/1/2017</date>
<amount>$45.93</amount>
</order>
<order id="OR3127" qty="4" custid="CUST091">
<date>5/2/2017</date>
<amount>$68.21</amount>
</order>
<order id="OR3128" qty="1" custid="CUST137">
<date>5/2/2017</date>
<amount>$117.24</amount>
</order>
<order id="OR3129" qty="1" custid="CUST128">
<date>5/3/2017</date>
<amount>$75.68</amount>
</order>
<order id="OR3130" qty="2" custid="CUST083">
<date>5/3/2017</date>
<amount>$58.93</amount>
</order>
<order id="OR3131" qty="1" custid="CUST304">
<date>5/3/2017</date>
<amount>$112.25</amount>
</order>
</orders>
&#13;
我得到的错误一直在说我必须&#34;终止或元素标记&#34;因为它是重复的东西......如果有人可以请求帮助,我将非常感谢...谢谢。
答案 0 :(得分:1)
XSLT必须是格式良好的XML,这意味着每个开始标记都必须有一个结束标记,但是你遗漏了很多。例如,当您创建 std::vector<int> crossref(SIZE) ; // SIZE is the size of the shared vector
std::iota (std::begin(crossref), std::end(crossref), 0); // Fill with indices ref
std::mt19937 g(SEED); // each thread has it own seed.
std::shuffle (crossref_.begin(), crossref_.end(), g); // Shuffle it
元素时,您可以执行此操作...
Obj.prop[idx] = newValue
但是没有可见的结束getattr(myObj, prop)[idx] = newValue
标记。如果您尝试缩进XSLT,使用带有开始标记的封闭标记,则会更加明显。
您的XSLT应该看起来像这样
>>> class CA:
... def __init__(self):
... self.x = [1,2,3]
...
>>> c = CA()
>>> getattr(c,'x')
[1, 2, 3]
>>> getattr(c,'x')[1] = 10
>>> getattr(c,'x')
[1, 10, 3]
请注意,此处并不需要qty
来创建元素名称。只需写出要直接输出的元素即可。试试这个
<xsl:element name="qty">
<xsl:value-of select="@qty" />
请注意在创建某些属性时使用Attribute Value Templates。花括号表示要计算的表达式,而不是字面输出。