为什么这个XSLT代码没有返回任何内容?

时间:2017-02-18 12:25:51

标签: html xml templates xslt

这是我的XML代码:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="2.xsl"?>

<xml_tube>
	<utilizadores>
		<utilizador username="Guicky" password="futurama96">
			<nome>Guilherme Luís</nome>
			<birthday>1996-11-28</birthday>
			<foto href="guilhas.jpg"/>
		</utilizador>
		<utilizador username="Daisy" password="woofwoof11">
			<nome>Daisy Luís</nome>
			<birthday>2011-04-04</birthday>
			<foto href="daisy.png"/>
		</utilizador>
		<utilizador username="Anabela65" password="crumble65">
			<nome>Anabela Ribeiro</nome>
			<birthday>1965-04-02</birthday>
			<foto href="belinha.jpg"/>
		</utilizador>
		<utilizador username="Izzie" password="lagartixa">
			<nome>Isadora Luís</nome>
			<birthday>1988-06-27</birthday>
			<foto href="izzie.jpg"/>
		</utilizador>
	</utilizadores>
	<videos_pub>
		<video id="Vid1" publisher="Daisy" duracao="3:37" data="2016-02-29">
			<título>Who let the dogs out? - Baha Men</título>
			<thumb>http://i3.ytimg.com/vi/Qkuu0Lwb5EM/hqdefault.jpg</thumb>
			<descrição>My favorite song, lol.</descrição>
			<hyperlink>https://www.youtube.com/watch?v=Qkuu0Lwb5EM</hyperlink>
			<qualidade pixelization="1080p"/>
			<qualidade pixelization="480p"/>
			<likes liked="Guicky"/>
			<related></related>
		</video>
		<video id="Vid2" publisher="Guicky" duracao="4:01" data="2016-02-29">
			<título>Desiigner - Timmy Turner</título>
			<thumb>http://i3.ytimg.com/vi/JzmRt2VgKYQ/hqdefault.jpg</thumb>
			<descrição>Timmy Timmy Timmy Turner.</descrição>
			<hyperlink>https://www.youtube.com/embed/ProbPpO_8oo</hyperlink>
			<qualidade pixelization="720p"/>
			<qualidade pixelization="1080p"/>
			<likes liked="Anabela65"/>
			<likes liked="Daisy"/>
			<related></related>
		</video>
		<video id="Vid3" publisher="Guicky" duracao="3:47" data="2016-02-29">
			<título>I'm all the way up - Fat Joe</título>
			<thumb>https://www.youtube.com/watch?v=y2ak_oBeC-I</thumb>
			<descrição>Nothing can stop me, I'm all the way up.</descrição>
			<hyperlink>https://www.youtube.com/embed/y2ak_oBeC-I</hyperlink>
			<qualidade pixelization="480p"/>
			<qualidade pixelization="720p"/>
			<related></related>
		</video>
		<video id="Vid4" publisher="Anabela65" duracao="3:21" data="2016-02-29">
			<título>Putzgrilla - Sentadinha</título>
			<thumb>http://i3.ytimg.com/vi/QC4JbIvIhdI/hqdefault.jpg</thumb>
			<descrição>Dá uma sentadinha.</descrição>
			<hyperlink>https://www.youtube.com/watch?v=QC4JbIvIhdI</hyperlink>
			<qualidade pixelization="720p"/>
			<likes liked="Guicky"/>
			<likes liked="Daisy"/>
			<related></related>
		</video>
		<video data="2017-01-13" duracao="02:16" id="Vid5" publisher="Izzie">
			<título>Yann Tiersen - La Valse d'Amelie</título>
			<thumb>http://i3.ytimg.com/vi/uzurqBnALkw/hqdefault.jpg</thumb>
			<descrição>A minha música preferida.</descrição>
			<hyperlink>https://www.youtube.com/watch?v=uzurqBnALkw</hyperlink>
			<qualidade pixelization="720p"/>
			<likes liked="Guicky"/>
			<likes liked="Anabela65"/>
			<related></related>
		</video>
	</videos_pub>
	<playlist>
		<lista owner="Guicky" dataIns="2016-10-24" id="PV1">
			<titulo>BEST MUSIC.</titulo>
			<descricao>Compilation of my favourite music videos.</descricao>
			<gostosL gostouL="Guicky"/>
			<links_vid vid="Vid2"/>
			<links_vid vid="Vid3"/>
			<administradores>
				<admin ref="Guicky"/>
				<admin ref="Daisy"/>
			</administradores>
			<editores>
				<editor ref="Guicky"/>
			</editores>
			<subscritores>
				<subs ref="Daisy"/>
				<subs ref="Anabela65"/>
			</subscritores>
			<thumbnail link="http://i3.ytimg.com/vi/QC4JbIvIhdI/hqdefault.jpg"/>
		</lista>
		<lista owner="Anabela65" dataIns="2016-02-29" id="PV2">
			<titulo>Sentadinha!</titulo>
			<descricao>Siting lesson's with Guicky's mom!</descricao>
			<links_vid vid="Vid4"/>
			<administradores>
				<admin ref="Anabela65"/>
				<admin ref="Guicky"/>
			</administradores>
			<editores>
				<editor ref="Anabela65"/>
			</editores>
			<subscritores>
				<subs ref="Guicky"/>
				<subs ref="Daisy"/>
			</subscritores>
			<thumbnail link="http://i3.ytimg.com/vi/uMK0prafzw0/hqdefault.jpg"/>
		</lista>
	</playlist>
	<comentarios>
		<comentario id="C1" refV="Vid1" user="Guicky" data="2016-10-23">
			<text>AHAHAHAHA, bom vídeo.</text>
			<gosto gostou="Daisy"/>
			<respostas>
				<texto autor="Daisy">Grande clássico!</texto>
			</respostas>
		</comentario>
		<comentario id="C2" refL="Vid2" user="Anabela65" data="2016-10-22">
			<text>Timmy timmy timmy turner...</text>
			<gosto gostou="Guicky"/>
			<gosto gostou="Daisy"/>
			<respostas>
				<texto autor="Guicky">U know it.</texto>
				<resposta autor="Daisy">LOL!</resposta>
			</respostas>
		</comentario>
	</comentarios>
</xml_tube>

这是我的XSLT代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
		<xsl:output method="xml" 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:imports href="1.xsl"/> -->
	<xsl:template match="/">
		<html>
			<head>
				<title>2.</title>
			</head>
			<body>
				<xsl:apply-templates select="//videos_pub"/>
				<!-- <xsl:apply-templates select="//comentarios"/> -->
			</body>
		</html>
	</xsl:template>
	
	
	<xsl:template match="videos_pub">
		<xsl:call-template name="publisher">
			<xsl:with-param name="id" select="//video/@id"/>
		</xsl:call-template>
		<xsl:call-template name="data">
			<xsl:with-param name="id" select="//video/@id"/>
		</xsl:call-template>
		<xsl:call-template name="comment">
			<xsl:with-param name="id" select="//video/@id"/>
		</xsl:call-template>
	</xsl:template>

	
	<xsl:template name="publisher">
		<xsl:param name="id"/>
		<p>
			<xsl:value-of select="//video[@id = $id]/./@publisher"/>
		</p>
	</xsl:template>
	
	<xsl:template name="data">
		<xsl:param name="id"/>
		<p>
			<xsl:value-of select="//video[@id= $id]/./@data"/>
		</p>
	</xsl:template>
	
	<xsl:template name="comment">
		<xsl:param name="id"/>
		<p>
			<xsl:value-of select="count(//comentario[@refV = $id])" />
		</p>
	</xsl:template>
</xsl:stylesheet>

它正在归还:

  

雏菊
  2016年2月29日
  0

这是我期望输出的内容:

  

雏菊
      2016年2月29日
      1

但它没有归还那个。为什么呢?
我是否必须退出模板//videos_pub
如果是这样,我该怎么做?

2 个答案:

答案 0 :(得分:0)

通过以下方式实现了:

<xsl:with-param name="id" select="//video/@id"/>

我选择的节点本身不是"//video/@id"表达式的值。

我通过这样做来修复它:

<xsl:with-param name="id">
    <xsl:value-of select="//video/@id"/>
</xsl:with-param>

现在我的param具有节点//video/@id的值。

谢谢大家的回答。

答案 1 :(得分:0)

你过度使用public static void closeConnection() { if (keepConnection) { keepConnection = false; try { bufferedReader.close(); printWriter.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } finally{ token = null; } } } 语法,当你不确定更好的方法时,这是一种使用XPath的“强力”方式。你也在使用“绕着你的肘部去找你的***”这样的表达,比如//

另外,我假设您要输出所有视频的信息,而不仅仅是第一个视频。

如果没有所有草率的东西,你将如何做到这一点:

//video[@id= $id]/./@data

如果您想提高效率,可以使用<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" 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:imports href="1.xsl"/> --> <xsl:template match="/"> <html> <head> <title>2.</title> </head> <body> <ul> <xsl:apply-templates select="xml_tube/videos_pub/video"/> </ul> <!-- <xsl:apply-templates select="//comentarios"/> --> </body> </html> </xsl:template> <xsl:template match="video"> <li> <xsl:call-template name="publisher"> <xsl:with-param name="video" select="."/> </xsl:call-template> <xsl:call-template name="data"> <xsl:with-param name="video" select="."/> </xsl:call-template> <xsl:call-template name="comment"> <xsl:with-param name="id" select="@id"/> </xsl:call-template> </li> </xsl:template> <xsl:template name="publisher"> <xsl:param name="video"/> <p> <xsl:value-of select="$video/@publisher"/> </p> </xsl:template> <xsl:template name="data"> <xsl:param name="video"/> <p> <xsl:value-of select="$video/@data"/> </p> </xsl:template> <xsl:template name="comment"> <xsl:param name="id"/> <p> <xsl:value-of select="count(/xml_tube/comentarios/comentario[@refV = $id])" /> </p> </xsl:template> </xsl:stylesheet> 来帮助计算评论:

xsl:key

这两个输出:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="xml" 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:key name="kVideoComments" match="comentario" use="@refV" />

  <!--  <xsl:imports href="1.xsl"/> -->
  <xsl:template match="/">
    <html>
      <head>
        <title>2.</title>
      </head>
      <body>
        <ul>
          <xsl:apply-templates select="xml_tube/videos_pub/video"/>
        </ul>
        <!-- <xsl:apply-templates select="//comentarios"/> -->
      </body>
    </html>
  </xsl:template>


  <xsl:template match="video">
    <li>
      <xsl:call-template name="publisher">
        <xsl:with-param name="video" select="."/>
      </xsl:call-template>
      <xsl:call-template name="data">
        <xsl:with-param name="video" select="."/>
      </xsl:call-template>
      <xsl:call-template name="comment">
        <xsl:with-param name="id" select="@id"/>
      </xsl:call-template>
    </li>
  </xsl:template>


  <xsl:template name="publisher">
    <xsl:param name="video"/>
    <p>
      <xsl:value-of select="$video/@publisher"/>
    </p>
  </xsl:template>

  <xsl:template name="data">
    <xsl:param name="video"/>
    <p>
      <xsl:value-of select="$video/@data"/>
    </p>
  </xsl:template>

  <xsl:template name="comment">
    <xsl:param name="id"/>
    <p>
      <xsl:value-of select="count(key('kVideoComments', $id))" />
    </p>
  </xsl:template>
</xsl:stylesheet>