我在firstName
和lastName
字段之间添加空格时遇到问题。我试图在一行上显示名字和姓氏,但它们之间没有空格。我该如何添加空间?这些字段来自我在SQL Server上的数据库。谢谢。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<style type="text/css">
.tutor
{
color: #000000;
font-family: "Arial";
font-size: large;
}
.data
{
font-family: "Arial";
font-size: large;
}
.heading
{
font-family: "Arial";
font-size: xx-large;
font-weight: bold;
color: #ABCDEF;
}
<title>Tutor Contact List</title>
</style>
</head>
<body>
<div class="heading">Tutor Contact List</div><br/>
<xsl:for-each select="list/tutorcontact">
<div class="tutor">Tutor Name:
<xsl:value-of select="lastname"/>
<xsl:value-of select="firstname"/></div>
<div class="data"><b>Phone:</b>
<xsl:value-of select="phone"/></div>
<div class="data"><b>E-Mail:</b>
<xsl:value-of select="email"/></div><br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:2)
尝试使用XPATH concat函数
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<style type="text/css">
.tutor
{
color: #000000;
font-family: "Arial";
font-size: large;
}
.data
{
font-family: "Arial";
font-size: large;
}
.heading
{
font-family: "Arial";
font-size: xx-large;
font-weight: bold;
color: #ABCDEF;
}
<title>Tutor Contact List</title>
</style>
</head>
<body>
<div class="heading">Tutor Contact List</div><br/>
<xsl:for-each select="list/tutorcontact">
<div class="tutor">Tutor Name:
<xsl:value-of select="concat(firstname,' ',lastname)"/>
</div>
<div class="data"><b>Phone:</b>
<xsl:value-of select="phone"/></div>
<div class="data"><b>E-Mail:</b>
<xsl:value-of select="email"/></div><br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
答案 1 :(得分:1)
<div class="tutor">Tutor Name:
<xsl:value-of select="lastname"/>
<xsl:value-of select="firstname"/></div>
在XSLT样式表中,默认情况下忽略包含仅空格的文本节点,除了
<xsl:text>
或xml:space="preserve"
因此,Tutor Name:
和lastname
之间的换行符和空格将被保留(因为虽然该文本节点包含空格,但它不包含仅空格)但是两个value-of
元素之间的元素将被忽略。如果你想在输出中的空格中在样式表中忽略它,那么你需要用
<div class="tutor">Tutor Name:
<xsl:value-of select="lastname"/>
<xsl:text> </xsl:text>
<xsl:value-of select="firstname"/></div>
但我个人只是使用方法suggested by OJay而只使用一个value-of
和concat(lastname, ' ', firstname)
。
答案 2 :(得分:0)
怎么样:
<div class="tutor">Tutor Name:
<xsl:value-of select="lastname"/>
<xsl:value-of select="firstname"/></div>
答案 3 :(得分:0)
使用Unicode不间断空格: 
或