尝试使用 XDT 和 XPath 定位器替换 XML 元素元素 [英] Trying to replace XML element element using XDT and XPath locator
问题描述
您好,我正在尝试使用 VS2012 XDT 替换 <cron-expression>
,方法是在quartz.net 作业描述文件上使用以下转换.我已经使用在线测试仪测试了 XPath 定位器,它返回我认为我需要的东西".任何帮助表示赞赏.
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>
对于示例 XML 文件 (Quartz.net)
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>
XPath//job-scheduling-data/schedule/trigger/cron[name='crontriggername2']
The XPath //job-scheduling-data/schedule/trigger/cron[name='crontriggername2']
返回正确的元素组(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>
我的表达式基于 Log4net 上另一个 XDT 问题的解决方案(log4Net 配置转换)
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.
推荐答案
另一种不需要对原始文件进行任何修改的替代解决方案是
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>
这篇关于尝试使用 XDT 和 XPath 定位器替换 XML 元素元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!