Html 在XSLT中,是否有一种在for-each循环中使用{}的方法?
我试图使用xslt创建html页面,但现在使用for-each元素遇到了一个障碍。有问题的线路如下Html 在XSLT中,是否有一种在for-each循环中使用{}的方法?,html,xml,xslt,xpath,xslt-2.0,Html,Xml,Xslt,Xpath,Xslt 2.0,我试图使用xslt创建html页面,但现在使用for-each元素遇到了一个障碍。有问题的线路如下 <xsl:for-each select="../../*:subscritores/subscritor[@userID = '{@userID}']/*:video"> 当代码的前面部分清楚地标记了用户时,有人知道我如何让for-each循环为不同的用户运行吗 如果我试图简单地打印出@userID,我会得到正确的结果,这意味着它应该按预期工作 我尝试解析xpath,它返回一个
<xsl:for-each select="../../*:subscritores/subscritor[@userID = '{@userID}']/*:video">
当代码的前面部分清楚地标记了用户时,有人知道我如何让for-each循环为不同的用户运行吗
如果我试图简单地打印出@userID,我会得到正确的结果,这意味着它应该按预期工作
我尝试解析xpath,它返回一个节点列表(如inteed所示)。我还尝试了用有效的userID替换{@userID}来强制执行它,结果很好,告诉我问题确实出在这一部分
完整代码如下:
XML-
XSL-
XSL(导入文件)-
而且,大多数数据都是用葡萄牙语写的,其余的完全是胡言乱语。如果有必要的话,我很乐意翻译,只要你觉得有必要
编辑-如有必要,DTD通常位于此处,似乎您有与属性值相关的变量元素,并且希望根据属性值跟踪交叉引用,因此XSLT中最有效的方法是使用
xsl:key
声明键,然后使用key
函数跟踪交叉引用,例如
<xsl:key name="subscritor-por-id" match="subscritor" use="@userID"/>
<xsl:key name="video-por-id" match="video" use="@videoID"/>
<xsl:key name="comentario-por-user" match="comentario" use="de/@userID"/>
然后代码(不包括,但应该显示原理方法)变为
Pagina Utilizador-Vista Longa
认购人
视频
-
科门塔里奥斯
-
请阅读:。仅从问题中的信息很难判断(我不打算查看pastebin链接),但请尝试将
'{@userID}'
更改为current()/@userID
。XPath表达式中不需要这样的大括号。通常您只想用$x
替换'{$x}'
;但在这种情况下,结果显然是胡说八道,所以你也有一个上下文问题。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xhtml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:output doctype-public="-//W3C//DTD XHTML 1.1//EN" doctype-system="http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<html xml:lang="pt">
<head>
<title>Pagina Utilizador - Vista Longa</title>
<link rel="stylesheet" type="text/css" href="../CSS.css" charset="UTF-8"/>
</head>
<body>
<div id="barra_de_navegacao">
<a href="../main.xhtml" class="button1">Pagina Inicial</a>
<a href="../listas/videos.html" class="button1">Vídeos</a>
<a href="../listas/lista_videos.html" class="button1">Lista de videos</a>
<a href="../listas/lista_users.html" class="button1">Lista de utilizadores</a>
</div>
<div class="display" id="user">
<xsl:apply-templates select="//user[@userID = 'user001']"/>
<!-- Para mudar utilizador, mudar aqui-->
</div>
</body>
</html>
</xsl:template>
<xsl:key name="subscritor-por-id" match="subscritor" use="@userID"/>
<xsl:key name="video-por-id" match="video" use="@videoID"/>
<xsl:key name="comentario-por-user" match="comentario" use="de/@userID"/>
<xsl:template match="user">
<xsl:apply-imports/>
<div class="user_subscricoes">
<h1>
<xsl:value-of select="@userID"/>
</h1>
<h3>Subscrições</h3>
<h3>Videos</h3>
<ul>
<!-- Comecando em user, precisamos subir na arvore, até á raiz para depois poder descer em direção aos subscritores-->
<xsl:for-each select="key('subscritor-por-id', @userID)/*:video">
<li>
<a href="../videos/{@videoID}.xhtml" class="button2">
<xsl:value-of select="key('video-por-id', @videoID)/nome"/>
</a>
</li>
</xsl:for-each>
</ul>
</div>
<div id="user_comentarios">
<!---->
<h1>
<xsl:value-of select="@userID"/>
</h1>
<!---->
<h3>Comentários</h3>
<ul>
<xsl:for-each select="key('comentario-por-user', @userID)">
<li>
<div class="button2">
<a href="../Vídeos/socialtube-vid001.xhtml"> <xsl:value-of select="/texto"/></a>
</div>
</li>
</xsl:for-each>
</ul>
</div>
</xsl:template>
</xsl:stylesheet>