Question

I'm trying to use the <TransformXml> task in my web projects (.csproj/.vbproj) to Transform configuration files and other XML files. I use an msbuild script (powershell) to build and package my code and generate environment-based transformed configuration and XML files.

This all works perfectly for simple usages such as Match, Condition, SetAttributes and so on, but I've recently tried to be a bit clever and use an XPath locator to find the last node in a group of nodes in order to only set the attributes of that last node. Additionally, I want to insert a new node after the last node, with some extra attributes.

When trying this syntax:

<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>

on XML (shortened) that looks approximately like this:

<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>

... I get the familiar (and expected) error

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

All the answers I can find for this revolve around using code to specify a Namespace manager, which makes perfect sense - but I have no code for this process, it's all within the scope of a TransformXml task and plain XML. I don't know which part of the XPath query is causing this error to occur, or how I can specify the type of task I need to run.

How can I accomplish this? Is it even possible to do this type of node manipulation within a transform?

Thanks

Edit: I can now see that the error is occurring on the InsertAfter call and not the SetAttributes - so it's this part that appears to be causing the issue.

Was it helpful?

Solution

Ok, so this was dead simple to resolve. "InsertAfter" takes an XPath expression directly and doesn't require the XPath function specifying - so this was a pretty simple fix! I'll leave this question up incase anyone is ever as impatient as I am :)

e.g. InsertAfter(/parent/a[last()])

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top