1

在使用 xslt 转换 xml 时,我得到了结果,就像贾斯汀一样,有人可以解释下面标记为粗体的语句吗?

xsl:value-of select=" //*[price='8.20']/@supplier "

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
        <cd supplier="justin">
        <title>Unchain my heart</title>
        <artist>Joe Cocker</artist>
        <country>USA</country>
        <company>EMI</company>
        <price>8.20</price>
        <year>1987</year>
    </cd>
</catalog>

xslt:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2>My Supplier</h2>
    <table border="1">
    <xsl:value-of select="//*[price='8.20']/@supplier"/
    </table>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>
4

2 回答 2

0

一般来说:

// - Traverse the whole document

//* - Any element in the document

//*[price = '8.20'] - Any element in the document that has a <price> child element
                      with the value 8.20

//*[price = '8.20']/@supplier - The @supplier attribute of any element in the document 
                                that has a <price> child element with the value 8.20

由于文档中只有一个节点符合这些条件,并且它的值为“justin”,这就是输出为“justin”的原因。

于 2013-02-11T17:42:04.310 回答
0

*: 任何元素

//:在任何层次结构(另一种方法是指定特定的 XPath

*[price='8.20']: 任何有子元素的元素price = '8.20'

//*[price='8.20']:具有子元素的任何层次结构中的任何元素price = '8.20'

@supplier: name 的属性supplier

//*/@suppliersupplier:任何层次结构中任何元素下的名称属性。

//*[price='8.20']/@suppliersupplier:具有子元素的任何层次结构中的任何元素下的名称属性price = '8.20'..

于 2013-02-11T17:46:30.063 回答