在XSLT谓词中使用XML元素值
我不完全确定XSLT是否可以做到这一点,我的讲师也不知道我在说什么,所以如果有人能帮忙,谢谢 我有一个XML文件,其中包含作者列表,每个作者都有一个唯一的id,还有一个书籍列表,每个书籍都有一个子元素,其值为IDREF。文件的布局如下所示:在XSLT谓词中使用XML元素值,xml,xslt,Xml,Xslt,我不完全确定XSLT是否可以做到这一点,我的讲师也不知道我在说什么,所以如果有人能帮忙,谢谢 我有一个XML文件,其中包含作者列表,每个作者都有一个唯一的id,还有一个书籍列表,每个书籍都有一个子元素,其值为IDREF。文件的布局如下所示: <library> <authors> <author id="a001"> <name>Joyce</name> </author> </a
<library>
<authors>
<author id="a001">
<name>Joyce</name>
</author>
</authors>
<books>
<book id="b001">
<name>Illiad</name>
<authorID>a001</authorID>
</book>
</books>
</library>
乔伊斯
伊利亚特
a001
我试图列出所有书籍的列表,并通过使用authord值获取有关作者的信息
XSLT:
XSLT测试
- 作者:
通过用一个值(例如a001)替换X,我可以让Joyce返回,但是我如何在这里使用authorID值,以便系统可以很好地扩展?您可以使用一个变量
<xsl:variable name="author" select="authorID/text()" />
然后在谓词中使用它
<xsl:value-of select="//author[@id=$author]/name"/>
您可以使用变量
<xsl:variable name="author" select="authorID/text()" />
然后在谓词中使用它
<xsl:value-of select="//author[@id=$author]/name"/>
- 作者:
- 作者:
避免使用/
操作员。它往往没有最好的性能特征,特别是当您像这样嵌套它时(对于每个//,值为//
)
使用规范的XPath表达式,如下所示。他们的表现要好得多:
<xsl:for-each select="/library/books/book">
<div style="display:block; background-color:#999; padding:2px; margin:2px;">
<h2><xsl:value-of select="name"/></h2>
<p><xsl:value-of select="synopsis"/></p>
<ul>
<li>
<xsl:text>Author: </xsl:text>
<!-- use the current() function to access the current node (book) -->
<xsl:value-of select="
/library/authors/author[@id=current()/authorID]/name
"/>
</li>
</ul>
</div>
</xsl:for-each>
这种方法具有迄今为止最好的性能,尤其是当输入文档变大时
您还应该避免使用
。尝试编写模板并改用模板匹配:
<xsl:template match="library">
<body>
<xsl:apply-templates select="books" />
</body>
</xsl:template>
<xsl:template match="books">
<h1>List of Books</h1>
<xsl:apply-templates select="book" />
</xsl:template>
<xsl:template match="book">
<div style="display:block; background-color:#999; padding:2px; margin:2px;">
<h2><xsl:value-of select="name"/></h2>
<p><xsl:value-of select="synopsis"/></p>
<ul>
<li>Author: <xsl:value-of select="key('kAuthorById', authorID)/name"/></li>
</ul>
</div>
</xsl:template>
书单
- 作者:
避免使用/
操作员。它往往没有最好的性能特征,特别是当您像这样嵌套它时(对于每个//,值为//
)
使用规范的XPath表达式,如下所示。他们的表现要好得多:
<xsl:for-each select="/library/books/book">
<div style="display:block; background-color:#999; padding:2px; margin:2px;">
<h2><xsl:value-of select="name"/></h2>
<p><xsl:value-of select="synopsis"/></p>
<ul>
<li>
<xsl:text>Author: </xsl:text>
<!-- use the current() function to access the current node (book) -->
<xsl:value-of select="
/library/authors/author[@id=current()/authorID]/name
"/>
</li>
</ul>
</div>
</xsl:for-each>
这种方法具有迄今为止最好的性能,尤其是当输入文档变大时
您还应该避免使用
。尝试编写模板并改用模板匹配:
<xsl:template match="library">
<body>
<xsl:apply-templates select="books" />
</body>
</xsl:template>
<xsl:template match="books">
<h1>List of Books</h1>
<xsl:apply-templates select="book" />
</xsl:template>
<xsl:template match="book">
<div style="display:block; background-color:#999; padding:2px; margin:2px;">
<h2><xsl:value-of select="name"/></h2>
<p><xsl:value-of select="synopsis"/></p>
<ul>
<li>Author: <xsl:value-of select="key('kAuthorById', authorID)/name"/></li>
</ul>
</div>
</xsl:template>
书单
- 作者:
+1对于key()
,我真的应该多用一次。+1对于key()
,我真的应该多用一次。
<xsl:template match="library">
<body>
<xsl:apply-templates select="books" />
</body>
</xsl:template>
<xsl:template match="books">
<h1>List of Books</h1>
<xsl:apply-templates select="book" />
</xsl:template>
<xsl:template match="book">
<div style="display:block; background-color:#999; padding:2px; margin:2px;">
<h2><xsl:value-of select="name"/></h2>
<p><xsl:value-of select="synopsis"/></p>
<ul>
<li>Author: <xsl:value-of select="key('kAuthorById', authorID)/name"/></li>
</ul>
</div>
</xsl:template>