首页 > 解决方案 > XPath 谓词仅使用节点集中的第一个节点进行匹配

问题描述

我正在尝试通过样式表中设置的静态节点过滤一些传入数据。我只想要文件节点包含我的文件节点之一的文本的 referencetemplate 元素。除了它似乎只检查第一个文件节点而不是所有文件节点这一事实之外,我所拥有的正在工作。我错过了什么?还是有更好的方法来做到这一点?

输入文件

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <referencetemplates>
        <referencetemplate>
            <name>Vascular Surgery Progress Note</name>
            <description>Vascular Surgery Progress Note</description>
            <file>\cernerbasiccontent\templates\standard templates\en\mun_vascular_surgery_progressnote.html</file>
            <source>cernerbasiccontent</source>
        </referencetemplate>
        <referencetemplate>
            <name>Video Visit Note</name>
            <description>Video Visit Note</description>
            <file>\cernerbasiccontent\templates\speciality templates\en\amb_video_visit.html</file>
            <source>cernerbasiccontent</source>
        </referencetemplate>
        <referencetemplate>
            <name>Well Child Office Note</name>
            <description>Well Child Office Note</description>
            <file>\cernerbasiccontent\templates\speciality templates\en\well_child_office_note.html</file>
            <source>cernerbasiccontent</source>
        </referencetemplate>
        <referencetemplate>
            <name>zzzz_IS_USE_ONLY</name>
            <description>zzzz_IS_USE_ONLY</description>
            <file>\cernerbasiccontent\templates\standard templates\en\mun_zzzz_is_use_only.html</file>
            <source>cernerbasiccontent</source>
        </referencetemplate>
    </referencetemplates>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">

    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" />

    <xsl:variable name="fileList">
        <file>care_management_office_visit_note.html</file>
        <file>ent_office_visit_note.html</file>
        <file>family_medicine_brief_office_visit_note.html</file>
        <file>family_medicine_office_visit_note.html</file>
        <file>gyn office visit_en.html</file>
        <file>ob_postpartum_office_note.html</file>
        <file>officeconsultnote_cardiology_en.html</file>
        <file>officeheartfailurenote_cardiology_en.html</file>
        <file>officestructuralheartnote_cardiology_en.html</file>
        <file>OfficeVisitNote_Cardiology_en.html</file>
        <file>OfficeVisitNote_Urology_en.html</file>
        <file>orthopedic_brief_office_note.html</file>
        <file>orthopedic_office_visit_note.html</file>
        <file>pediatric_office_visit_note.html</file>
        <file>physical_medicine_rehab_office_visit_note.html</file>
        <file>podiatry_office_visit_note.html</file>
        <file>pre_procedure_consultation_note.html</file>
        <file>prenatal intake office visit_en.html</file>
        <file>prenatal office visit_en.html</file>
        <file>surgical_brief_office_note.html</file>
        <file>surgical_office_visit_note.html</file>
        <file>teen_health_clinic_note_template.html</file>
        <file>well_child_office_note.html</file>
    </xsl:variable>

    <xsl:template match="/">
        <referencetemplates>
            <xsl:variable name="fileNodeSet" select="msxsl:node-set($fileList)/file" />
            <xsl:attribute name="count">
                <xsl:value-of select="count(root/referencetemplates/referencetemplate[contains(file, $fileNodeSet)])" />
            </xsl:attribute>
            <xsl:for-each select="root/referencetemplates/referencetemplate[contains(file, $fileNodeSet)]">
                <xsl:copy-of select="." />
            </xsl:for-each>
        </referencetemplates>
    </xsl:template>

</xsl:stylesheet>

编辑: 我能够通过不依赖该contains功能来使其工作。幸运的是,我能够将完整的文件路径放入我的 $fileList 变量中,然后简单地进行直接相等比较。我仍然对为什么使用contains不起作用感兴趣......

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">

    <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" />

    <xsl:variable name="fileList">
        <file>\cernerbasiccontent\templates\speciality templates\en\care_management_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\ent_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\family_medicine_brief_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\family_medicine_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\gyn office visit_en.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\ob_postpartum_office_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\officeheartfailurenote_cardiology_en.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\officestructuralheartnote_cardiology_en.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\officevisitnote_cardiology_en.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\officevisitnote_urology_en.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\orthopedic_brief_office_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\orthopedic_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\pediatric_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\physical_medicine_rehab_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\podiatry_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\prenatal intake office visit_en.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\prenatal office visit_en.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\surgical_brief_office_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\surgical_office_visit_note.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\teen_health_clinic_note_template.html</file>
        <file>\cernerbasiccontent\templates\speciality templates\en\well_child_office_note.html</file>
    </xsl:variable>

    <xsl:template match="/">
        <referencetemplates>
            <xsl:variable name="fileNodeSet" select="msxsl:node-set($fileList)/file" />
            <xsl:attribute name="count">
                <xsl:value-of select="count(root/referencetemplates/referencetemplate[file = $fileNodeSet])" />
            </xsl:attribute>
            <xsl:for-each select="root/referencetemplates/referencetemplate[file = $fileNodeSet]">
                <xsl:copy-of select="." />
            </xsl:for-each>
        </referencetemplates>
    </xsl:template>

</xsl:stylesheet>

标签: xmlxslt

解决方案


我仍然对为什么使用contains不起作用感兴趣......

它不起作用,因为该contains()函数将两个字符串作为参数。您使用节点集作为第二个参数来调用它 - 这会导致(在 XSLT 1.0 中)通过获取集合中第一个节点的字符串值将节点集转换为字符串(在 XSLT 2.0 中,您将得到一个错误)。

请参阅此处了解两种可能的解决方案:
https ://stackoverflow.com/a/65362565/3016153


推荐阅读