我做了一个xslt的简单演示。我想打印费率为video_urls
的{{1}}代码值。我为外部标记中的每个循环添加并获取标记值?
Сan我添加了嵌套的25
循环以获取其费率为foreach
的网址?
这是我的源XML:
25
XSLT代码:
<catalog>
<cd>
<title>Empire Burlesque</title>
<urls>
<video_urls rate ="25">
abccc
</video_urls>
<video_urls rate ="30">
sdfsdf
</video_urls>
</urls>
</cd>
<cd>
<title>Unchain my heart</title>
<video_urls rate ="25">
123nnn
</video_urls>
<video_urls rate ="30">
pppppp
</video_urls>
</cd>
</catalog>
预期输出
<xsl:template match="/">
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:value-of select="urls/video_urls/@rate='25'"/>
</xsl:for-each>
</xsl:template>
答案 0 :(得分:1)
您只能直接访问具有特定属性值的值,如下所示
您正试图错误地访问该属性,
urls/video_urls/@rate= "25"
相反,它应该是访问和检查,
urls/video_urls[@rate = 25]
您的XML也与节点不一致,因此模板无法提供您所需的预期答案。
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/catalog">
<xsl:for-each select="cd">
<xsl:value-of select="title"/>
<xsl:value-of select="urls/video_urls[@rate = 25]"/>
</xsl:for-each>
</xsl:template>
</xsl:transform>
您还需要修复XML以保持一致性。
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<urls>
<video_urls rate ="25">
abccc
</video_urls>
<video_urls rate ="30">
sdfsdf
</video_urls>
</urls>
</cd>
<cd>
<title>Unchain my heart</title>
<urls>
<video_urls rate ="25">
123nnn
</video_urls>
<video_urls rate ="30">
pppppp
</video_urls>
</urls>
</cd>
</catalog>