我刚开始学习XML & XSLT并对Xpath有一个简短的问题。XSLT Xpath通配符
这里的XML代码:
<root>
<shop>
<person>
<employee>
<name> Alexis </name>
<role> Manager </role>
<task> Sales </task>
</employee>
</person>
</shop>
<person>
<employee>
<role> Supervisor </role>
<name> Blake </name>
<task> Control </task>
</employee>
</person>
</root>
和这里的XSLT代码:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<html><head></head>
<body><xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="shop">
<xsl:apply-templates select="/root/*/*"/>
</xsl:template>
<xsl:template match="employee">
<u> <xsl:apply-templates select="name"/> </u>
(Task: <xsl:apply-templates select="task"/>)
<br></br>
</xsl:template>
<xsl:template match="person2">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
输出是:
Alexis (Task: Sales)
Blake (Task: Control)
Blake (Task: Control)
我不明白为什么最后一部分是重复的?我知道,这是由于XSLT这部分代码:
<xsl:apply-templates select="/root/*/*"/>
但那只是因为我是用代码摆弄周围,并在Firefox中显示它。我不明白为什么。
从我的理解,它的选择所有孙子“根”的元素,就像这样:
根/店/人
但为什么不亚历克西斯重复呢?只有布雷克重复...
+1尼斯解释和替代样式表。 –
非常感谢您的回复! 我不想让它只显示员工或任何东西,它只是一个我想了解的考试问题。 我了解你: /根/店/人及/根/人/员工 但由于员工的人对于这部分孩子: /根/店/人/员工 那么为什么Alexis也不是重复输出的一部分吗? 此外,这是做什么*/* ?? – shadowz1337
我已经扩展了我的答案,希望能够尝试并多解释一下,以及'*/*'的含义。 –