简体   繁体   中英

Applying XDT transform based on sibling element value

Given this XML config file:

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" version="2.0">
  <schedule>
    <trigger>
      <cron>
        <name>Trigger1</name>
        <cron-expression>0 0 1 * * ?</cron-expression>
      </cron>
    </trigger>
    <trigger>
      <cron>
        <name>Trigger2</name>
        <cron-expression>0 0 2 * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

I want to transform <cron-expression> value for Trigger2 .

Using this transform:

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <schedule>
    <trigger>
      <cron>
        <name>Trigger2</name>
        <cron-expression 
          xdt:Transform="Replace" 
          xdt:Locator="XPath(/job-scheduling-data/schedule/trigger/cron[name='Trigger2'])">0 0 3 * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

When the project builds, I see compiler warning:

No element in the source document matches '/job-scheduling-data/schedule/trigger/cron[name='Trigger2']'.

The XPath query seems correct, but the rule is never applied (hence the compiler warning).

My project is configured to transform on build using TransformXml task in Visual Studio 2015.

Am I doing something wrong?

@PingCrosby answer to another question helped solve the problem. The solution is:

<job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <schedule>
    <trigger>
      <cron xdt:Locator="XPath(//*[local-name()='job-scheduling-data']
                                /*[local-name()='schedule']
                                /*[local-name()='trigger']
                                /*[local-name()='cron']
                                  [*[local-name() = 'name'] = 'Trigger2'])">
        <cron-expression xdt:Transform="Replace">0 0 3 * * ?</cron-expression>
      </cron>
    </trigger>
  </schedule>
</job-scheduling-data>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM