Search code examples
xmlxpathquartz.netxdt-transformxdt

Trying to replace XML element element using XDT and XPath locator


Hi I am trying to replace the <cron-expression> using VS2012 XDT by using the following transform on a quartz.net job description file. I have tested the XPath locator using an online tester and it returns 'what i think i need'. Any help is appreciated.

<schedule>
    <trigger>
        <cron xdt:Locator="XPath(//job-scheduling-data/schedule/trigger/cron[name='crontriggername2'])" >
            <cron-expression  xdt:Transform="Replace">***some data***</cron-expression>
        </cron>
    </trigger>
</schedule>

for the sample XML file (Quartz.net)

  <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"><processing-directives>
    <overwrite-existing-data>true</overwrite-existing-data>
</processing-directives>

<schedule>
       <trigger>
        <cron>
            <name>crontriggername1</name>
            <group>crontriggergroup1</group>
            <description>crontriggerdesc1</description>
            <job-name>crontriggerjobname1</job-name>
            <job-group>crontriggerjgroup1</job-group>
            <misfire-instruction>crontriggermisfile1</misfire-instruction>
            <cron-expression>0/5 * * * * ?</cron-expression>
        </cron>
    </trigger>

    <trigger>
        <cron>
            <name>crontriggername2</name>
            <group>crontriggergroup2</group>
            <description>crontriggerdesc2</description>
            <job-name>crontriggerjobname2</job-name>
            <job-group>crontriggerjgroup2</job-group>
            <misfire-instruction>crontriggermisfile2</misfire-instruction>
            <cron-expression>0/13 * * * * ?</cron-expression>
        </cron>
    </trigger>      
</schedule>

The XPath //job-scheduling-data/schedule/trigger/cron[name='crontriggername2']

returns me the correct element group (http://www.freeformatter.com/xpath-tester.html1)

<cron>
  <name>crontriggername2</name>
  <group>crontriggergroup2</group>
  <description>crontriggerdesc2</description>
  <job-name>crontriggerjobname2</job-name>
  <job-group>crontriggerjgroup2</job-group>
  <misfire-instruction>crontriggermisfile2</misfire-instruction>
  <cron-expression>0/13 * * * * ?</cron-expression>
</cron>

I based the expression on the solution to another XDT quesion on Log4net (log4Net config transform)

Can anybody point me to a correct solution, this is driving me insane. Thanks in advance.


Solution

  • An alternative solution that does not need any modifications to the original file 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'] = 'MyTriggerName'])">
    
    or this syntax...
    
            <cron xdt:Locator="XPath(//*[local-name()='job-scheduling-data' and namespace-uri()='http://quartznet.sourceforge.net/JobSchedulingData']
                                     /*[local-name()='schedule' and namespace-uri()='http://quartznet.sourceforge.net/JobSchedulingData']
                                     /*[local-name()='trigger' and namespace-uri()='http://quartznet.sourceforge.net/JobSchedulingData']
                                     /*[local-name()='cron' and namespace-uri()='http://quartznet.sourceforge.net/JobSchedulingData']
                                       [*[local-name() = 'name'] = 'MyTriggerName'])">
    
                <cron-expression  xdt:Transform="Replace">***some data***</cron-expression>
            </cron>
        </trigger>
    </schedule>
    

    see also I am trying to get the Xpath for a value but getting error for nested condition