试图为其他人解决问题,却遇到了我自己的奇怪问题,我确信这很简单,但答案却让我难以捉摸!
我拥有的 XML:
<xml>
<head>
<info>
<content>
<source attribute1="RSC1985s5c1" attribute2="6(17)">
data1
</source>
<cite/>
<case/>
(
<target attribute1="LRC1985s5c1" attribute2="6(17)1">
3e/191
</target>
)
</content>
<content>
<source attribute1="RSC1985s5c1" attribute2="6(17)">
data2
</source>
<cite/>
<case/>
(
<target attribute1="LRC1985s5c4" attribute2="6(17)1">
4e/54
</target>
)
</content>
</info>
</head>
</xml>
用 XSLT 来组合两个内容元素:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="info">
<xsl:copy>
<xsl:element name="content">
<xsl:for-each select="content">
<xsl:apply-templates select="@*"/>
<xsl:apply-templates select="node()"/>
</xsl:for-each>
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这将创建:
<?xml version="1.0" encoding="utf-8"?><xml> //Just noticed here too!
<head>
<info><content> //Why no new line here?
<source attribute1="RSC1985s5c1" attribute2="6(17)">
data1
</source>
<cite />
<case />
(
<target attribute1="LRC1985s5c1" attribute2="6(17)1">
3e/191
</target>
)
<source attribute1="RSC1985s5c1" attribute2="6(17)">
data2
</source>
<cite />
<case />
(
<target attribute1="LRC1985s5c4" attribute2="6(17)1">
4e/54
</target>
)
</content></info> //And again here?
</head>
</xml>
使用评论突出显示的问题是我无法弄清楚的,为什么元素之间没有新线。
提前致谢。