在 XSLT 中使用正则表达式 [英] Using RegEx in XSLT
问题描述
我需要解析 Visual Studio 自动生成的 XML 文档来创建报告.我决定使用 XSLT,但我对它很陌生,需要帮助.常用模板为:
I need to parse Visual Studio automatically generated XML documentation to create a report. I decided to use XSLT but I'm very new to it and need help. Common template is:
<doc>
<members>
<member name="F:MyNamespace">
<summary>Some text</summary>
</member>
</members>
</doc>
我想隔离名称以某个单词开头的成员,例如 P:Interfaces.Core.我决定在 select 语句中使用 RegExp.
I want to isolate members with name which begins on some word, for example, P:Interfaces.Core. I decided to use RegExp in select statement.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/TR/xpath-functions/">
<xsl:template match="/" >
<html xmlns="http://www.w3.org/1999/xhtml">
<body style="font-family:Tahoma">
<p>Interfaces list:</p>
<table>
<xsl:for-each select="doc/members/member">
<xsl:sort order="ascending" />
<xsl:value-of select="fn:matches(., 'P\..+')" />
<br />
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
为什么我收到错误:
命名空间 http://www.w3.org/TR/xpath-functions 不包含任何功能 >
Namespace http://www.w3.org/TR/xpath-functions does not contain any functions >
我哪里错了?我在示例中找到了这样的代码,包括 w3c.org!
Where am I wrong? I found such code in examples, including w3c.org!
推荐答案
如果您使用 Visual Studio X 执行转换,其中 X 不大于 2008,这将由XSLT 1.0 处理器(.NET 的 XslCompiledTransform
或XslTransform
强>).XSLT 1.0 使用 XPath 1.0,而不是 XPath 2.0 及其F &O(函数和操作),去年才成为 W3 推荐.
In case you're performing the transformation with Visual Studio X, where X is not greater than 2008, this would be processed by an XSLT 1.0 processor (.NET's XslCompiledTransform
or XslTransform
). XSLT 1.0 uses XPath 1.0, not XPath 2.0 and its F & O (Functions and Operations), which only became a W3 Recommendation last year.
您有两个选择:
使用兼容的 XSLT 2.0 处理器.如果您更喜欢留在 .NET 平台内,那么合适的选择是 Saxon.NET一个>
Use a compliant XSLT 2.0 processor. If you prefer to stay within the .NET platform, then a suitable choice is Saxon.NET
只需使用 XPath 1.0 功能 starts-with()
,足以解决目前的问题.
表达式:starts-with(., 'P:Interfaces')
如果上下文节点的字符串值以字符串 'P:Interfaces' 和 false()
否则.
Just use the XPath 1.0 function starts-with()
, which is sufficient to solve the current problem.
The expression: starts-with(., 'P:Interfaces')
is evaluated to true()
if the string value of the context node starts with the string 'P:Interfaces' and to false()
otherwise.
对于此类处理可能会派上用场的另一个 Xpath 1.0 函数是函数 contains()
.
Another Xpath 1.0 function that may come handy for such type of processing is the function contains()
.
Xpath 的 2.0 函数 ends-with()
可以通过以下方式在 XPath 1.0 中模拟:
Xpath's 2.0 function ends-with()
can be emulated in XPath 1.0 in the following way:
ends-with(s1, s2)
====substring
(s1,string-length
(s1)-字符串长度
(s2)+1)=s2
ends-with(s1, s2)
====substring
(s1,string-length
(s1)-string-length
(s2)+1)=s2
其中===
"的意思是等价于".
where "===
" means is "equivalent to".
这里我们还使用了 XPath 1.0 函数 substring()
和 string-length()
.
Here we also used the XPath 1.0 functions substring()
and string-length()
.
这篇关于在 XSLT 中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!