当一个exerice要求我将文本从输入复制到输出时,我经常输入以下代码:
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
但我注意到它几乎总是没用的。有没有我必须使用此代码的情况,或者它总是没用的?
例如,在这种情况下,无论是否使用这三行代码,输出都是相同的:输入:
<z>
<d>testo 0
<rad>testo 1</rad>
</d>
<b>
<p>
<w>
<z>testo 2</z>
</w>
</p>
</b>
<d>
<p/>
<y>testo 3
<d>testo 4</d>
</y>
<p>testo 5
<d>testo 6</d>
</p>
<p/>
</d>
<z/>
</z>
期望输出:
<z>
<nuovo livelloInput="figlio radice">testo 0
<rad livelloInput="nipote radice">testo 1</rad>
</nuovo>
<nuovo livelloInput="figlio radice">
<p livelloInput="nipote radice">testo 2</p>
</nuovo>
<nuovo livelloInput="figlio radice">
<p livelloInput="nipote radice"/>
<y livelloInput="nipote radice">testo 3 testo 4</y>
<p livelloInput="nipote radice">testo 5 testo 6</p>
<p livelloInput="nipote radice"/>
</nuovo>
<nuovo livelloInput="figlio radice"/>
</z>
XSLT 代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:template match="/*">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="/*/*">
<nuovo>
<xsl:attribute name="livelloinput">figlioradice</xsl:attribute>
<xsl:apply-templates></xsl:apply-templates>
</nuovo>
</xsl:template>
<xsl:template match="/*/*/*">
<xsl:element name="{name()}">
<xsl:attribute name="livelloInput">nipoteradice</xsl:attribute>
<xsl:apply-templates></xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="text()">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
由于内置模板规则,这是文本节点的默认行为。因此,您并非绝对需要该模板。