【问题标题】:Web config transformation condition/match to select a node based on parent node attributeWeb 配置转换条件/匹配以根据父节点属性选择节点
【发布时间】:2023-03-16 15:13:01
【问题描述】:

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

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

这是无效的。

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

【问题讨论】:

  • 如果完全删除 xdt:Locator 会发生什么?对我来说,只要父级指定了 name 属性,转换就可以正常工作,就像你在这里一样......

标签: xpath web-config-transform


【解决方案1】:

答案是 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>

【讨论】:

  • 太棒了!我总是在同一个节点上与他们合作,但这种方式解决了我遇到的一些问题。
  • 谢谢!我试图进行插入,但它一直添加到第一个父级,而不是插入的当前父级。这
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多