基于属性值的XSL复制节点(如搜索)

时间:2017-01-29 19:11:34

标签: xslt-1.0

我附加了XML文件,我只想复制属性值为的东西,在我的例子中是AHC_。在每个节点中,任何属性都具有以' AHC _'开头的值。复制该节点并忽略其他节点。

我正在尝试使用以下XSL并且条件正在运行但没有复制节点。请你看看并建议吗?

 <?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>

示例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>

预期输出:

<?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 个答案:

答案 0 :(得分:0)

  

条件正在运行,但没有复制节点。

那不是真的。复制所有Schedule个节点,没有条件。那是因为你的情况毫无意义。这没有任何意义,因为它与你所说的你想要的无关:

  

在每个节点中,任何属性都具有以'AHC_'复制的值开头的值   节点

更重要的是,它没有意义,因为它是从父Schedules元素的上下文中评估的,而不是单个Schedule节点。因此,如果任何 Schedule孩子满足条件,您将应用模板(即复制)所有

要选择性地仅复制满足某些条件的Schedule元素,您应该将样式表构造为:

<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>