我有这个结构
<ROWS>
<ROW>
<TEXT> This is a @good@ @day@ </TEXT>
<good>great</good>
<day>month</day>
</ROW>
<ROW>
<TEXT> This is a @good@ @day@ </TEXT>
<good>Fun</good>
<day>morning</day>
</ROW>
</ROWS>
如何将其更改为
<statement> This is a great month, this is a Fun morning </statement>
仅使用XSLT 1.0?
原始XML可以更改标签名称。但不是结构!有什么想法吗?
这似乎与从模板创建表单字母有些相似。假设该示例不是字面意思,您可以尝试以下操作:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<statements>
<xsl:for-each select="ROWS/ROW/TEXT">
<statement>
<xsl:call-template name="merge">
<xsl:with-param name="string" select="."/>
</xsl:call-template>
</statement>
</xsl:for-each>
</statements>
</xsl:template>
<xsl:template name="merge">
<xsl:param name="string"/>
<xsl:param name="sep" select="'@'"/>
<xsl:choose>
<xsl:when test="contains($string, $sep) and contains(substring-after($string, $sep), $sep)">
<xsl:value-of select="substring-before($string, $sep)" />
<xsl:variable name="placeholder" select="substring-before(substring-after($string, $sep), $sep)" />
<xsl:value-of select="../*[name() = $placeholder]" />
<!-- recursive call -->
<xsl:call-template name="merge">
<xsl:with-param name="string" select="substring-after(substring-after($string, $sep), $sep)" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
给出了:
的输入<ROWS>
<ROW>
<TEXT>The quick brown @animal@ jumps over the @property@ dog.</TEXT>
<animal>fox</animal>
<property>lazy</property>
</ROW>
<ROW>
<TEXT>A journey of a @number@ miles @action@ with a single @act@.</TEXT>
<number>thousand</number>
<action>begins</action>
<act>step</act>
</ROW>
</ROWS>
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<statements>
<statement>The quick brown fox jumps over the lazy dog.</statement>
<statement>A journey of a thousand miles begins with a single step.</statement>
</statements>
查看XSLT 1.0 Spec;在字符串函数上找到部分。研究包含,底字节和底字节之后的函数。解决问题的解决方案应该变得清晰;如果不是这样,您至少应该能够在您的问题上得到足够的范围,以提出一个问题,看起来似乎可以解释为"请为我做我的功课。"