【问题标题】:XSL - match text of node and append another node as siblingXSL - 匹配节点的文本并将另一个节点附加为兄弟节点
【发布时间】:2012-06-06 19:19:46
【问题描述】:

我有这样的 XML:

<a>
  <b>
    <c>some text here</c>
  </b>
</a>
<a>
  <b>
    <c>another cool text</c>
  </b>
</a>

我需要在 //a/b/c/text() 中匹配单词“cool”并像这样转换 XML

<a>
  <b>
    <c>some text here</c>
  </b>
</a>
<x>
  <y> prepend to cool text </y>
</x>
<a>
  <b>
    <c>another cool text</c>
  </b>
</a>

我试过了:

<xsl:template
        match="//a/b/c/matches(text(),'\.*cool\*.')">
...
</xsl:template>

但没有运气:

XTSE0340: XSLT Pattern syntax error at char 153 on line 30 in {...\.*cool\*...}:
  Function call may appear only at the start of a pattern

我在这里错过了什么?

【问题讨论】:

    标签: java xslt xslt-2.0


    【解决方案1】:

    你的匹配不正确,它应该在括号内测试[]:

    <xsl:template
            match="//a/b/c[matches(text(), '.*cool.*')]">
    ...
    </xsl:template>
    

    另外,如果你的正则表达式这么简单,那么你可以使用 XPath 方法contains()

    【讨论】:

    • 谢谢,我正在寻找的东西,在我的情况下并不是那么简单,所以我真的需要正则表达式。无论如何,感谢您的快速回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多