XSLT操纵分散在各个节点上的文本(XSLT manipulate text scattered across various nodes)

输入文件如下:

<?xml version="1.0" encoding="UTF-8"?> <!-- lower UPPER case --> <document> <rubbish> rubbish </rubbish> <span class='lower'> lower <span class='upper'> upper </span> case </span> </document>

通缉输出:

lower UPPER case

我知道如何使用value-of来获取包含在外部span的文本,但是这也包括字符串"upper"不变,这不是我想要的。 我不知道如何操纵内部span的文本并将其插入其他文本的中间。

尝试失败:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" indent="no"/> <xsl:template match="/"> <xsl:for-each select="//span[@class = 'lower']"> <xsl:if test="span/@class = 'upper'"> <xsl:text>do something</xsl:text> <!--TO DO --> </xsl:if> <xsl:value-of select="."/> </xsl:for-each> </xsl:template> </xsl:stylesheet>

Input file as follows:

<?xml version="1.0" encoding="UTF-8"?> <!-- lower UPPER case --> <document> <rubbish> rubbish </rubbish> <span class='lower'> lower <span class='upper'> upper </span> case </span> </document>

Wanted output:

lower UPPER case

I know how to get the text included in the outer span with value-of, but this also includes the string "upper" unchanged which is not what I want. I do not know how to manipulate the text in the inner span and insert it in the middle of the other text.

Failed attempt:

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="text" indent="no"/> <xsl:template match="/"> <xsl:for-each select="//span[@class = 'lower']"> <xsl:if test="span/@class = 'upper'"> <xsl:text>do something</xsl:text> <!--TO DO --> </xsl:if> <xsl:value-of select="."/> </xsl:for-each> </xsl:template> </xsl:stylesheet>

最满意答案

你需要在这里采取递归方法,例如:

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text" encoding="UTF-8"/> <xsl:template match="text()[parent::span]"> <xsl:choose> <xsl:when test="../@class='upper'"> <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="." /> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>

要了解其工作原理,请阅读内置模板规则: http : //www.w3.org/TR/xslt/#built-in-rule

You need to take a recursive approach here, for example:

XSLT 1.0

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:output method="text" encoding="UTF-8"/> <xsl:template match="text()[parent::span]"> <xsl:choose> <xsl:when test="../@class='upper'"> <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" /> </xsl:when> <xsl:otherwise> <xsl:value-of select="." /> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="text()"/> </xsl:stylesheet>

To understand how this works, read up on built-in template rules: http://www.w3.org/TR/xslt/#built-in-rule

更多推荐