1

不太确定这种方法的重要性,但我只是想尝试一下。不是直接在 C# 中使用 XPath 来使用 XML DOM 和读取值,是否可以将字符串作为参数传递给 XSLT 并在 XSLT 中将其用作 XPath?

C#:

.........
......

XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("XPathTest", "", SomedynamicString);
XsltSettings xslsettings = new XsltSettings(false, true);
xslTransform.Load(string_xslInput, xslsettings, new XmlUrlResolver());
xslTransform.Transform(xpathXmlOrig, xslArg, objMemoryStream);
.........
......

XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:crnObj="urn:UTCCalc">
  <xsl:output method="xml" indent="no"/>
  <xsl:param name="XPathTest"/>
.........
.......

  <xsl:template match=??$XPathTest$$??> How can the param be used?
     <!--code goes here-->
  </xsl:template>
.........
.......
4

3 回答 3

2

Instead of using XML DOM and reading Value by using XPath directly in C#, Is it possible to pass string as a param to XSLT and in XSLT use it as XPath?

Not in XSLT 1.0 or in XSLT 2.0.

In XSLT 3.0 (still a working draft) one may use the xsl:evaluate instruction.

It may be interesting to you to see how the XPath Visualizer does this: the XPath expression is used to dynamically modify the value of a specific xsl:variable -- even before the transformation is invoked.

Disclaimer:

The XPath Visualizer is not for sale and/or profit -- I created it 12 years ago for fun and solely with educational purposes.

于 2012-11-09T13:02:53.980 回答
1

如果您的 XSLT 处理器实现 xx:evaluate() 扩展来评估作为字符串提供的 XPath 表达式,它将起作用。这是一个很常见的扩展,但它在 XSLT 1.0 或 2.0 中都不是标准的。

于 2012-11-09T12:37:50.517 回答
0

.NET 转换引擎支持扩展 - 您可以通过以下方式做到这一点:

  • 使用命名空间字符串和类的实例创建XsltArgumentList和调用;AddExtensionObject
  • 使用在样式表中引用相同的命名空间字符串xmlns:prefix=
  • 通过简单地在样式表中声明它们的(限定)名称来调用扩展上的方法,即value-of select='prefix:MyMethod'
于 2012-11-09T12:48:38.590 回答