XSLT 选择除一个子节点文本之外的所有文本

标签 xslt select text nodes except

我有这样的 xml:

<article>
   <title> Test title - <literal> Compulsory - </literal> <fn> ABC </fn> 
   <comments> a comment</comments>
   </title>
</article>

我想在变量中获取所有子节点+自身文本 例如

$full_title = "Test title - Compulsory - ABC"

Except comments node text.

Following is my unsuccessful try where i miss title node text.

<xsl:template name="test">
    <xsl:variable name="full_title" select="article/title/*[not(self::comments)][1]" />
    <xsl:variable name="width" select="45" /> 
                <xsl:choose>
                    <xsl:when test="string-length($full_title) &gt;    $width">
                        <xsl:value-of select="concat(substring($full_title,1,$width),'..')"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$full_title"/>    
                    </xsl:otherwise>
            </xsl:choose>
</xsl:template>

最佳答案

更改 *node() .这将选择作为 <title> 子元素的元素和文本节点。元素。然后取出[1]因为你想要 <title>所有个 child :

<xsl:variable name="full_title"
    select="string-join(article/title/node()[not(self::comments)], '')" />

一种更可靠的方法,这样如果您在 <title> 下有多个级别,您就不会被绊倒和 <comments>元素作为孙子出现,将是这样的:

<xsl:variable name="full_title"
    select="string-join(article/title//text()[not(ancestor::comments)], '')" />

更新:

由于您希望变量保存一个字符串值,并且由于您将它传递给类似 concat() 的函数和 string-length()它不能将多个节点的序列作为第一个参数,使用 string-join(..., '')围绕序列通过连接每个节点的字符串值将其转换为字符串。

关于XSLT 选择除一个子节点文本之外的所有文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17995770/

相关文章:

xml - 使用 < 和 > 括号转换 XML CDATA 中的 HTML

xml - 如何有条件地转换 XML 节点,而不忽略此条件之外的实例?

xml - XSLT 不复制 HTML 实体的值

html - 选择元素从 Firefox 中的页面溢出

sql - SAS:左连接适用于定义的数字,但不适用于选定的数字列表

html - 如何将css添加到xsl文件中?

mysql - 在子查询中使用主查询中的列时出错

android - 水平翻转 TextView

iphone - iPad/iPhone CSS 文本 : Stretch 100%?

flutter - TextBaseline 的字母和表意枚举在 Flutter 中不起作用