Tech Wizard
Tech Wizard

Reputation: 435

TransformXml fails to find a node due to namespace

I have custom config file that is transformed via msbuild TransformXml. The top node has xlmns attribute. I can't remove it. Here is the sample of TestSettings.Debug.config:

<TestSettings xmlns="http://example.com" type="mytype, mydll"
xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<Config>
    <servers>
        <main>
            <add Name="PROD" Description="Production Server" URL="http://myserver.example.com"
                 DefaultPort="" Environment="Production" xdt:Transform="Insert" xdt:Locator="Match(Name)"/>
        </main>
    </servers>
    <BusinessDayStart xdt:Transform="InsertBefore(//BusinessDayEnd)">PT7H0M</BusinessDayStart>
    <Senders>
        <Sender >
            <Email xdt:Transform="InsertAfter(//Sender[1]/Signature)" >[email protected]</Email>
        </Sender>
    </Senders>
</Config>

Here is sample of Source file:

<TestSettings xmlns="http://example.com" type="mytype, mydll">
<Config>
    <servers>
        <main>
        </main>
    </servers>
    <BusinessDayEnd>PT7H0M</BusinessDayEnd>
    <Senders>
        <Sender>
            <CompanyId>CompanyID</CompanyId>
            <Signature> My Company Service </Signature>
        </Sender>
    </Senders>
</Config>

TransformXml throws "No element in the source document matches"

I updated both files to have explicit namespace xmlns:kk="http://example.com" and all nodes and XPath to be prefixed with "kk:" i.e.

<kk:BusinessDayStart xdt:Transform="InsertBefore(//kk:BusinessDayEnd)">PT7H0M</kk:BusinessDayStart>

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

I read that in 2010 there was a bug with namespaces that was resolved. No matter what I tried nothing helped.

Please help to resolve it.

Upvotes: 0

Views: 280

Answers (1)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

You can skip default namespaces by using the local-name() function, e.g.,

//*[local-name()='BusinessDayEnd']

and

//*[local-name()='Sender'][1]/*[local-name()='Signature']

Upvotes: 4

Related Questions