首页 > 解决方案 > 如何检查特定属性是否存在于 XML 中的所有子节点中?

问题描述

如何检查 XML 中的所有子节点中是否存在特定属性?例如,我想检查 ws:Status 的所有子节点中是否存在 ws:PriorValue ?

<ws:Status>
<ws:Staffing_Event ws:PriorValue="">LOA</ws:Staffing_Event>
<ws:Staffing_Event_Date ws:PriorValue="">2020-05-01</ws:Staffing_Event_Date>
<ws:Employee_Status ws:PriorValue="Active">OnLeave</ws:Employee_Status>
<ws:Active ws:PriorValue="true">false</ws:Active>
<ws:Hire_Date>2000-01-01</ws:Hire_Date>
</ws:Status>

标签: xsltxslt-2.0xslt-3.0

解决方案


你可以这样做:

<xsl:template match="ws:Status">
    <test>
        <xsl:if test="*[not(@ws:PriorValue)]">Not all child elements have a PriorValue attribute</xsl:if>
    </test>
</xsl:template>

推荐阅读