xpath - Web配置转换条件/匹配以基于父节点属性选择节点

标签 xpath web-config-transform

我有一个看起来像这样的变换

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <a>
    <b>
      <c>
        <d>
          <e name="UpdateLanguageProfile">
            <f xdt:Transform="Replace" xdt:Locator="Condition(/..@name='UpdateLanguageProfile')">
              stuff here
            </f>
          </e>
        </d>
      </c>
    </b>
  </a>

因此,仅当父节点具有具有指定值的属性时,我才希望xdt:Locator选择f节点。

xdt:Locator被转换为以下xpath表达式:
/a/b/c/d/e/f[/..@name='UpdateLanguageProfile']

哪个无效。

所以问题是,为了根据父节点中的属性选择f节点,我可以在Condition中添加什么,即XPath方括号。

最佳答案

答案是xdt:Locator和xdt:Transform不必在同一节点上。在我所见过的每个示例中,它们恰好位于同一节点上。

你可以这样做:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <a>
    <b>
      <c>
        <d>
          <e name="UpdateLanguageProfile" xdt:Locator="Match(name)">
            <f xdt:Transform="Replace">
              stuff here
            </f>
          </e>
        </d>
      </c>
    </b>
  </a>

关于xpath - Web配置转换条件/匹配以基于父节点属性选择节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13886291/

相关文章:

java - 通过遍历<tr id>从后代标签的getAttribute中获取ID,然后遍历到td,然后输入标签以获取其id

xml - 在 XPATH 中使用多个命名空间

xml - XSLT将线性结构转换为非线性

asp.net - Web Config 转换以添加子元素

asp.net - Web.config 构建与发布转换不起作用

xml - XSL身份副本

php - 使用 xpath PHP 获取具有多个值的元素中的某些属性值

msbuild - 如何在转换时将 configSource 转换为 web.config 中的内联元素

visual-studio-2012 - 针对不同环境进行 web.config 转换时出错

asp.net - Web 转换文件被复制到 root + bin