在 XSLT 中的元素之间添加换行符
Adding newline between elements in XSLT
我开始使用 XML 和 XSLT,我在添加新行 beetwen 元素时遇到问题
这是XML:
<?xml version="1.0" encoding="UTF-8"?>
<numbers>
<person id="1">
<phone>
<phone_nr>111111111</phone_nr>
<phone_nr>222222222</phone_nr>
</phone>
</person>
<person id="2">
<phone>
<phone_nr>333333333</phone_nr>
</phone>
</person>
</numbers>
XSLT 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="numbers/person">
<table border="1">
<tr>
<td>
<table>
<td><xsl:value-of select="phone"/></td>
</table>
</td>
</tr>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
它给了我这个(带边框):
111111111 222222222
333333333
但我想要的是:
111111111
222222222
333333333
问题是 XML 必须是这样的,我不知道如何在 XSLT 中创建新行。
您正在输出 HTML,因此要执行 "newline" 您需要输出一个 <br>
标记。您目前遇到的问题是您正在输出 phone
元素的文本值,它将其下的所有文本节点连接在一起。您确实需要单独处理子 phone_nr
节点,例如 xsl:for-each
<td>
<xsl:for-each select="phone/phone_nr">
<xsl:value-of select="."/><br />
</xsl:for-each>
</td>
试试这个 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="numbers/person">
<table border="1">
<tr>
<td>
<xsl:for-each select="phone/phone_nr">
<xsl:value-of select="."/><br />
</xsl:for-each>
</td>
</tr>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如果不知道 exact 输出应该是什么,很难回答您的问题。按照您向我们展示的内容,最简单的方法是:
<xsl:template match="/numbers">
<table border="1">
<xsl:for-each select="person/phone/phone_nr">
<tr>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
我开始使用 XML 和 XSLT,我在添加新行 beetwen 元素时遇到问题
这是XML:
<?xml version="1.0" encoding="UTF-8"?>
<numbers>
<person id="1">
<phone>
<phone_nr>111111111</phone_nr>
<phone_nr>222222222</phone_nr>
</phone>
</person>
<person id="2">
<phone>
<phone_nr>333333333</phone_nr>
</phone>
</person>
</numbers>
XSLT 看起来像:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="numbers/person">
<table border="1">
<tr>
<td>
<table>
<td><xsl:value-of select="phone"/></td>
</table>
</td>
</tr>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
它给了我这个(带边框):
111111111 222222222
333333333
但我想要的是:
111111111
222222222
333333333
问题是 XML 必须是这样的,我不知道如何在 XSLT 中创建新行。
您正在输出 HTML,因此要执行 "newline" 您需要输出一个 <br>
标记。您目前遇到的问题是您正在输出 phone
元素的文本值,它将其下的所有文本节点连接在一起。您确实需要单独处理子 phone_nr
节点,例如 xsl:for-each
<td>
<xsl:for-each select="phone/phone_nr">
<xsl:value-of select="."/><br />
</xsl:for-each>
</td>
试试这个 XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="numbers/person">
<table border="1">
<tr>
<td>
<xsl:for-each select="phone/phone_nr">
<xsl:value-of select="."/><br />
</xsl:for-each>
</td>
</tr>
</table>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如果不知道 exact 输出应该是什么,很难回答您的问题。按照您向我们展示的内容,最简单的方法是:
<xsl:template match="/numbers">
<table border="1">
<xsl:for-each select="person/phone/phone_nr">
<tr>
<td><xsl:value-of select="."/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>