如果子节点丢失,则插入配置

时间:2019-05-13 10:41:43

标签: xslt xpath slowcheetah

我正在尝试使用Microsoft.Web.XmlTransform.dll更新xml文档。如果一个带有子元素的属性名称不匹配,我需要插入一个新的Assembly元素。

转换:

<config>
  <assembly xdt:Transform="Insert" xdt:Locator="Condition(./assembly/file/@name!='qux.dll')>
    <file name='qux.dll>
  </assembly>
</config>

原始配置:

<config>
  <assembly>
    <file name='foo.dll>
  </assembly>
  <assembly>
    <file name='bar.dll>
  </assembly>
  <assembly>
    <file name='qux.dll>
  </assembly>
</config>

输出:

<config>
  <assembly>
    <file name='foo.dll>
  </assembly>
  <assembly>
    <file name='bar.dll>
  </assembly>
  <assembly>
    <file name='qux.dll>
  </assembly>
  <assembly>
    <file name='qux.dll>
  </assembly>
</config>

预期输出:

<config>
  <assembly>
    <file name='foo.dll>
  </assembly>
  <assembly>
    <file name='bar.dll>
  </assembly>
  <assembly>
    <file name='qux.dll>
  </assembly>
</config>

我假设xdt:Locator =“ Condition(./ assembly/file/@name!='qux.dll')不正确,但我终生无法按预期工作。

1 个答案:

答案 0 :(得分:0)

破解:

<config>
  <assembly xdt:Transform="InsertIfMissing" xdt:Locator="Condition(file/@name='qux.dll')>
    <file name='qux.dll>
  </assembly>
</config>