xml - 将模板应用于 XSLT 中的子字符串

标签 xml xslt substring apply-templates

想象一下我的 XML 文件如下所示:

<root>
  <test>
    Lorem ipsum dolor sit amet, <randomTag>consectetur</randomTag>
    adipiscing elit, sed do <randomTag>eiusmod</randomTag> tempor
    incididunt ut labore et dolore magna aliqua...
  </test>
</root>

以下代码不起作用:

<xsl:template match="/">
  <xsl:apply-templates select="substring(test,1,50)"/> ...
</xsl:template>

<xsl:template match="randomTag">
  <myTag><xsl:value-of select="."/></myTag>
</xsl:template>

我期望以下输出:

Lorem ipsum dolor sit amet, <myTag>consectetur</myTag> 
adipiscing...

如果我用 test 替换 substring(test,1,50) ,它可以工作,但我只想要前 50 个字符。

我也尝试过以这种方式使用变量:

<xsl:template match="/">
  <xsl:variable name="aux" select="substring(test,1,50)"/>
  <xsl:apply-templates select="$aux"/> ...
</xsl:template>

但即使这样也有效。

在我看来,问题出在 substring() 表达式上。 有什么建议吗?

最佳答案

<xsl:apply-templates>仅适用于 node-set ,即原始文档中的一组节点。

在许多 XSLT 处理器中,您可以创建额外的 node-setexsl:node-set扩展功能。

关于xml - 将模板应用于 XSLT 中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575016/

相关文章:

c# - 在任何第一个数字之前拆分字符串

java - 在 Android 中访问基于 Soap 的 Web 服务

python - 在读取此 xml 之前,如何检查特定元素值 - django python

xslt - 转换 xsl :apply-templates into a string value to use as an if parameter

mysql - 在字符串中查找数字值并使用替换增加它

c - 是否有一种优雅的行业标准方式在 C 中实现 substr ?

c# - 将 Linq.Table 序列化为 XML

xml - SQL Server 2008 查询 Soap XML

java - 如何分解 <fo :table-block> present inside <fo:table-cell> 的内容

ruby-on-rails - 导出事件记录,包括 Rails 中的关系和附件