2

我是 XML 新手。我正在尝试创建包含项目详细信息的表和另一个表以包含选项列表上每个订单的客户详细信息。看起来应该很简单,但我只是得到了所有订单上所有项目的列表,这些订单按订单数量重复。我究竟做错了什么?(下面的 XSL 代码...)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" doctype-system="about:legacy-compat"/>
<xsl:template match="/">

<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" type="css/text" href="style.css"/>
<title>Orders</title>
</head>

<body>
<xsl:for-each select="//order">
<table>
<caption><h3>Order Information</h3></caption>
<thead>
<th align="left">Item Id</th>
<th align="left">Item Description</th>
<th align="left">Quantity</th>
<th align="left">Price</th>
</thead>
<xsl:for-each select="//item">
<tr>
<td align="left"><xsl:value-of select="itemId"/></td>
<td align="left"><xsl:value-of select="itemName"/></td>
<td align="left"><xsl:value-of select="quantity"/></td>
<td align="left"><xsl:value-of select="price"/></td>
</tr>
</xsl:for-each>
</table>
<table>
<caption><h3>Customer Information</h3></caption>
<thead>
<th align="left">Customer Name</th>
<th align="left">Street</th>
<th align="left">City</th>
</thead>
<xsl:for-each select="//item">
<tr>
<td align="left"><xsl:value-of select="customerName"/></td>
<td align="left"><xsl:value-of select="street"/></td>
<td align="left"><xsl:value-of select="city"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</body>
</html>

</xsl:template>
</xsl:stylesheet>

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Orders.xsl"?>

<orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Orders.xsd">
<order>
<orderId>123</orderId>
<items>
<item>
<itemId>001</itemId>
<itemName>Nylon Rope</itemName>
<quantity>1</quantity>
<price>3.50</price>
</item>
<item>
<itemId>002</itemId>
<itemName>Shovel</itemName>
<quantity>1</quantity>
<price>24.95</price>
</item>
</items>
<customerAddress>
<customerName>Larry Murphy</customerName>
<street>Shallowgrave Lane</street>
<city>Ballymore Eustace, Co. Kildare</city>
</customerAddress>
</order>
<order>
<orderId>124</orderId>
<items>
<item>
<itemId>001</itemId>
<itemName>Whiskey</itemName>
<quantity>1</quantity>
<price>18.50</price>
</item>
<item>
<itemId>002</itemId>
<itemName>Shotgun</itemName>
<quantity>1</quantity>
<price>225</price>
</item>
<item>
<itemId>003</itemId>
<itemName>Cartridge</itemName>
<quantity>1</quantity>
<price>1.85</price>
</item>
</items>
<customerAddress>
<customerName>Enda Kenny</customerName>
<street>A Avenue</street>
<city>Castlebar, Co. Mayo</city>
</customerAddress>
</order>
</orders>

相关 XSD:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsd:element name="orders">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="1" ref="order"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="1" ref="items"/>
<xsd:element maxOccurs="1" minOccurs="1" ref="customerAddress"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="items">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="1" ref="item"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="item">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" minOccurs="1" ref="itemId"/>
<xsd:element maxOccurs="unbounded" minOccurs="1" ref="itemName"/>
<xsd:element maxOccurs="unbounded" minOccurs="1" ref="quantity"/>
<xsd:element maxoccurs="unbounded" minOccurs="1" ref="price"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:element name="customerAddress">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" ref="customerName"/>
<xsd:element maxOccurs="1" minOccurs="1" ref="street"/>
<xsd:element maxOccurs="1" minOccurs="1" ref="city"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="itemId" type="xsd:string"/>
<xsd:element name="itemName" type="xsd:string"/>
<xsd:element name="quantity" type="xsd:int"/>
<xsd:element name="price" type="xsd:double"/>
<xsd:element name="customerName" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>

</xsd:schema>
4

3 回答 3

3
<xsl:for-each select="//order">

选择<order>文档中的所有标签。

<xsl:for-each select="//item">

将同样选择<item>文档中的所有标签。这就是为什么你会得到你看到的结果。

你需要的是

<xsl:for-each select=".//item">

解释是//它本身只是descendant-or-self根元素的意思。.//另一方面,表示当前元素(在本例中为<order>元素)的后代或自身,因此现在您的项目将被正确分组。

您可以(并且确实应该)在此处阅读更多相关信息,了解轴和上下文节点对于了解 XSLT/XPath 的工作原理至关重要,并且将为您省去很多痛苦。

于 2012-10-15T22:58:40.920 回答
1

不要使用 // - 这样您总是从 xml 文档中选择所有元素,而不是只与上下文节点相关的元素。

于 2012-10-15T22:58:18.453 回答
0

尝试这样的事情(未经测试,也许也用模板/应用模板替换两个内部循环):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" doctype-system="about:legacy-compat" />
    <xsl:template match="/">
        <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
                <meta charset="utf-8" />
                <link rel="stylesheet" type="css/text" href="style.css" />
                <title>Orders</title>
            </head>

            <body>
                <xsl:apply-templates select="//order" />
            </body>
        </html>
    </xsl:template>
    <xsl:template match="//order">
        <table>
            <caption>
                <h3>Order Information</h3>
            </caption>
            <thead>
                <th align="left">Item Id</th>
                <th align="left">Item Description</th>
                <th align="left">Quantity</th>
                <th align="left">Price</th>
            </thead>
            <xsl:for-each select="//item">
                <tr>
                    <td align="left">
                        <xsl:value-of select="itemId" />
                    </td>
                    <td align="left">
                        <xsl:value-of select="itemName" />
                    </td>
                    <td align="left">
                        <xsl:value-of select="quantity" />
                    </td>
                    <td align="left">
                        <xsl:value-of select="price" />
                    </td>
                </tr>
            </xsl:for-each>
        </table>
        <table>
            <caption>
                <h3>Customer Information</h3>
            </caption>
            <thead>
                <th align="left">Customer Name</th>
                <th align="left">Street</th>
                <th align="left">City</th>
            </thead>
            <xsl:for-each select="//item">
                <tr>
                    <td align="left">
                        <xsl:value-of select="customerName" />
                    </td>
                    <td align="left">
                        <xsl:value-of select="street" />
                    </td>
                    <td align="left">
                        <xsl:value-of select="city" />
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

我通常使用模板来实现可重用性:

<xsl:template match="Menu">
<xsl:template match="MenuItem">

然后在我需要它们的地方:

<xsl:apply-templates select="/MenuRoot/Menu" />
<xsl:apply-templates select="/MenuRoot/MenuItem" />

在您的情况下,您可以使用match="/order/item"and match="/order/customer/item"(只是对结构的疯狂猜测),尝试更具体。

于 2012-10-15T23:02:57.367 回答