Xml 在XSLT中的元素之间添加换行符
我正在学习XML和XSLT,在添加新的元素行时遇到了问题 以下是XML:Xml 在XSLT中的元素之间添加换行符,xml,xslt,newline,Xml,Xslt,Newline,我正在学习XML和XSLT,在添加新的元素行时遇到了问题 以下是XML: <?xml version="1.0" encoding="UTF-8"?> <numbers> <person id="1"> <phone> <phone_nr>111111111</phone_nr> <phone_nr>222222222</phone_nr>
<?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>
但我想要的是:
111111111
222222222
333333333
问题是XML必须是这样的,我不知道如何在XSLT中创建新行。您正在输出HTML,所以要创建“新行”,您需要输出一个
标记。目前的问题是输出phone
元素的文本值,该元素将其下的所有文本节点连接在一起。您确实需要分别处理子phone\u 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>
如果不知道确切的输出应该是什么,就很难回答您的问题。按照你给我们展示的,最简单的方法是:
<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>
请将您的预期结果作为代码发布。
<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>
<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>