我需要解析 Visual Studio 自动生成的 XML 文档来创建报告。我决定使用 XSLT,但我对它很陌生并且需要帮助。
常见的模板是:
<doc>
<members>
<member name="F:MyNamespace">
<summary>Some text</summary>
</member>
</members>
</doc>
我想隔离名称以某个单词开头的成员,例如 P:Interfaces.Core。我决定在 select 语句中使用 RegExp。
<?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 http://www.w3.org/TR/xpath-functions不包含任何功能>
我哪里错了?我在示例中找到了这样的代码,包括 w3c.org!
如果您使用 Visual Studio X 执行转换,其中 X 不大于 2008,这将由XSLT 1.0 http://www.w3.org/TR/xslt处理器(.NET 的XslCompiledTransform http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx or XslTransform http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltransform.aspx)。 XSLT 1.0 的用途XPath 1.0 http://www.w3.org/TR/xpath, not XPath 2.0 http://www.w3.org/TR/xpath20/和它的餐饮 http://www.w3.org/TR/xpath-functions/(功能和操作),去年才成为 W3 推荐标准。
你有两个选择:
使用兼容的 XSLT 2.0 处理器。如果您更喜欢留在 .NET 平台内,那么合适的选择是撒克逊网 http://saxon.sourceforge.net/
只需使用 XPath 1.0 函数 starts-with() http://www.w3.org/TR/xpath#section-String-Functions,足以解决当前问题。
表达方式:starts-with(., 'P:Interfaces')
被评估为true()
如果上下文节点的字符串值以字符串“P:Interfaces”开头并且false()
否则。
另一个 Xpath 1.0 函数对于这种类型的处理可能会派上用场,它是函数contains() http://www.w3.org/TR/xpath#section-String-Functions.
Xpath的2.0功能ends-with() http://www.w3.org/TR/xpath-functions/#func-ends-with可以通过以下方式在 XPath 1.0 中进行模拟:
ends-with(s1, s2)
====substring
(s1,string-length
(s1)-string-length
(s2)+1)=s2
where "===
” 的意思是“相当于”。
这里我们还使用了XPath 1.0函数substring() http://www.w3.org/TR/xpath#section-String-Functions and string-length() http://www.w3.org/TR/xpath#section-String-Functions.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)