由于命名空间,TransformXml 找不到节点

TransformXml fails to find a node due to namespace

我有通过 msbuild TransformXml 转换的自定义配置文件。 顶级节点具有 xlmns 属性。 我无法删除它。 这是 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)" >abc@example.com</Email>
        </Sender>
    </Senders>
</Config>

这是源文件的示例:

<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 抛出 "No element in the source document matches"

我更新了两个文件以具有明确的命名空间 xmlns:kk="http://example.com" 所有节点和 XPath 都以 "kk:" 为前缀,即

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

我遇到错误 "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function"

我读到 2010 年有一个命名空间错误已解决。 无论我尝试什么都没有帮助。

请帮忙解决

您可以使用 local-name() 函数跳过默认名称空间,例如,

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

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