问题
I have attached XML file and I want to copy only the node which attribute value is something, In my case AHC_. In each node any attribute has value starting with 'AHC_' copy that node and ignore other nodes.
I am trying with following XSL and condition is working but not copying the node.Could you please take a look and suggest?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="UTF-8" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Schedules">
<xsl:if test="Schedule[@ServiceName='MESAVisToolkit_RetentionProcessor']">
<xsl:apply-templates select="@*|node()"/>
</xsl:if>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Sample XML:
<?xml version="1.0" encoding="UTF-8"?>
<Schedules>
<Schedule OrganizationKey=" " ScheduleID="13" ServiceName="RetentionProcessor">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<time>0200</time>
</times>
</day>
</days>
<excludedDates/>
</TimingXML>
</Schedule>
<Schedule OrganizationKey=" " ScheduleID="14" ServiceName="MESAVisToolkit_RetentionProcessor">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<time>0300</time>
</times>
</day>
</days>
</TimingXML>
</Schedule>
<Schedule OrganizationKey=" " ScheduleID="15" ServiceName="MailboxEvaluateAllAutomaticRules">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<timeRange>
<range>0000-2359</range>
<interval>1</interval>
<onMinute>0</onMinute>
</timeRange>
</times>
</day>
</days>
<excludedDates/>
</TimingXML>
</Schedule>
<Schedule OrganizationKey=" " ScheduleID="16" ServiceName="MailboxEvaluateAllAutomaticRulesSubMin">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<timeRange>
<range>0000-2359</range>
<interval>1</interval>
<onMinute>0</onMinute>
</timeRange>
</times>
</day>
</days>
</TimingXML>
</Schedule>
<Schedule OrganizationKey=" " ScheduleID="51" ServiceName="AHC_001_01_0100_Get_Schedule">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<timeRange>
<range>0000-2359</range>
<interval>5</interval>
<onMinute>0</onMinute>
</timeRange>
</times>
</day>
</days>
</TimingXML>
</Schedule>
<Schedule OrganizationKey=" " ScheduleID="54" ServiceName="AHC_001_01_0200_Get_Schedule">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<timeRange>
<range>0000-2359</range>
<interval>5</interval>
<onMinute>0</onMinute>
</timeRange>
</times>
</day>
</days>
</TimingXML>
</Schedule>
</Schedules>
Expected Output:
<?xml version="1.0" encoding="UTF-8"?>
<Schedule OrganizationKey=" " ScheduleID="51" ServiceName="AHC_001_01_0100_Get_Schedule">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<timeRange>
<range>0000-2359</range>
<interval>5</interval>
<onMinute>0</onMinute>
</timeRange>
</times>
</day>
</days>
</TimingXML>
</Schedule>
<Schedule OrganizationKey=" " ScheduleID="54" ServiceName="AHC_001_01_0200_Get_Schedule">
<TimingXML>
<days>
<day ofWeek="-1">
<times>
<timeRange>
<range>0000-2359</range>
<interval>5</interval>
<onMinute>0</onMinute>
</timeRange>
</times>
</day>
</days>
</TimingXML>
</Schedule>
</Schedules>
回答1:
condition is working but not copying the node.
That's not true. All Schedule
nodes are copied, without condition. That's because your condition makes no sense. It makes no sense because it has nothing to do with what you said you want:
In each node any attribute has value starting with 'AHC_' copy that node
Even more importantly, it makes no sense because it is evaluated from the context of the parent Schedules
element, not the individual Schedule
nodes. Thus, if any Schedule
child satisfies the condition, you will be applying templates to (i.e. copying) all of them.
To selectively copy only Schedule
elements that satisfy some condition, you should structure your stylesheet as:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/Schedules">
<xsl:copy>
<xsl:copy-of select="Schedule[--your condition goes here---]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
来源:https://stackoverflow.com/questions/41924861/xsl-copy-nodes-based-on-attribute-value-like-search