我想在我的web项目(.csproj/.vbproj)中使用<TransformXml>
任务来转换配置文件和其他XML文件。我使用一个msbuild脚本(powershell)来构建和打包我的代码,并生成基于环境的转换配置和XML文件。带有InsertAfter的TransformXml任务 - 如何使用函数?
这一切都完全适用于简单的用途,例如Match
,Condition
,SetAttributes
等等,但我最近想有点聪明和使用的XPath定位找到最后一个节点一组的节点,以便只有设置最后一个节点的属性。另外,我想在最后一个节点之后插入一个新节点,并带有一些额外的属性。
在尝试此语法:
<parent>
<a xdt:Transform="SetAttributes(from)" xdt:Locator="XPath(/a[ancestor-or-self::section[last()]])" from="20130522" />
<a xdt:Transform="SetAttributes(to)" xdt:Locator="XPath(/a[ancestor-or-self::section[last()]])" to="20130630" />
<a xdt:Transform="InsertAfter(XPath(/a[ancestor-or-self::section[last()]]))" from="20130701" to="20140101" />
</parent>
XML(可缩短),看起来大约是这样的:
<parent>
<a from="xxx1" to="yyy1">
<one>one</one>
<two>two</two>
</a>
<a from="xxx2" to="yyy2">
<one>one</one>
<two>two</two>
</a>
<a from="xxx3" to="yyy3">
<one>one</one>
<two>two</two>
</a>
</parent>
...我得到了熟悉的(和预期)错误
Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
我能找到的所有答案都围绕着我们用于指定名称空间管理器,这是非常有意义的 - 但我没有为这个过程编写代码,它全部在TransformXml
任务和普通XML的范围内。我不知道XPath查询的哪一部分会导致此错误发生,或者我可以如何指定我需要运行的任务的类型。
我该如何做到这一点?是否有可能在变换中执行这种类型的节点操作?
感谢
编辑:我现在可以看到的是,错误发生在InsertAfter调用,而不是SetAttributes - 所以它的这一部分,似乎是导致该问题。