我做了一个简单的XSLT演示。我想打印速率为 25
的 video_urls
标签值。我在外部标签中为每个循环添加并获取标签值?
我会添加嵌套的foreach
循环以获取url的率是25
?
这是我的源XML:
<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>
XSLT代码:
<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>
预期输出
Empire Burlesque
abccc
Unchain my heart
123nnn
您只能直接访问具有特定属性值如下的那些值,
您试图错误地访问属性,
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>
更新链接:http://xsltransform.net/jz1pups/3