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



我已经附加了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>

条件有效,但未复制节点。

那不是真的。无条件复制所有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>

相关内容

  • 没有找到相关文章

最新更新