如何使用XSLT找到具有某些属性值的所有嵌套XML节点



我的多级深XML(长期为示例非常抱歉),在各个级别上包含错误消息的节点。这些节点都具有@ID的属性,以" _ERROR_MESSAGE"结尾。我正在尝试使用XSLT SELECT将所有这些错误收获到一个平面列表中,但是由于某种原因,Select只能找到两个这样的节点,而根本无法获取其文本。我在做什么错?

结果:

<root>
   <myErrorTest/>
   <myErrorTest/>    
</root>

xslt

<xsl:template match="global-instance">
    <xsl:for-each select="@* | node()[contains('_error_message',@id)]">
         <xsl:element name="myErrorTest"><xsl:value-of select="text()"/></xsl:element>
    </xsl:for-each>
</xsl:template>
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

xml

<global-instance>
    <entity id='form'>
        <outcome id="form_error_message">xxx</outcome>
        <outcome id="payment-due"/>
        <entity id="sectionA">
            <instance id="090">
                <outcome id="sectionA_error_message">sss</outcome>
                <outcome id="sectionA_name">schedule A</outcome> 
                <entity id="lineA">
                    <lineNumber>1A</lineNumber>
                    <instance id="123">
                        <entity id="fieldA"> 
                            <instance id="3456">
                                <outcome id="fieldA_error_message">rrr</outcome>
                                <outcome id="fieldA_name">tax id</outcome>
                            </instance>
                        </entity>
                        <outcome id="lineA_error_message">ttt</outcome>
                        <outcome id="lineA_name">first line</outcome> 
                    </instance>      
                </entity>  
                <entity id="lineB">
                    <lineNumber>1B</lineNumber>
                    <instance id="127">
                        <entity id="field"> 
                            <instance id="3535">
                                <outcome id="fieldB_error_message">qqq</outcome>
                                <outcome id="fieldB_name">schedule A</outcome>
                            </instance>
                        </entity>
                        <outcome id="lineB_error_message">bbb</outcome>
                        <outcome id="lineB_name">tax number</outcome> 
                    </instance>      
                </entity> 
            </instance>     
        </entity>
        <entity id="sectionB">
            <instance id="727">
                <outcome id="sectionB_error_message">sss</outcome>
                <outcome id="sectionB_name">schedule A</outcome> 
                <entity id="lineA">
                    <lineNumber>1A</lineNumber>
                    <instance id="124">
                        <entity id="fieldA"> 
                            <instance id="3446">
                                <outcome id="fieldA_error_message">rrr</outcome>
                                <outcome id="fieldA_name">tax id</outcome>
                            </instance>
                        </entity>
                        <outcome id="lineA_error_message">ttt</outcome>
                        <outcome id="lineA_name">first line</outcome> 
                    </instance>      
                </entity>  
                <entity id="lineB">
                    <lineNumber>1B</lineNumber>
                    <instance id="133">
                        <entity id="field"> 
                            <instance id="3355">
                                <outcome id="fieldB_error_message">qqq</outcome>
                                <outcome id="fieldB_name">schedule A</outcome>
                            </instance>
                        </entity>
                        <outcome id="lineB_error_message">bbb</outcome>
                        <outcome id="lineB_name">tax number</outcome> 
                    </instance>      
                </entity>
            </instance>      
        </entity>
    </entity>
</global-instance>

您有contains的论点错误的方式,而您的select仅在查看global-instance的直接子女,而不是更深入的后代。

<xsl:template match="global-instance">
    <xsl:for-each select=".//*[contains(@id,'_error_message')]">
         <myErrorTest><xsl:value-of select="."/></myErrorTest>
    </xsl:for-each>
</xsl:template>

相关内容

  • 没有找到相关文章

最新更新