1
votes

I have multi-level deep XML (terribly sorry for the long example) with nodes on various level containing error messages. These nodes all have attribute @id ending with "_error_message". I'm trying to harvest all these errors into a flat list with XSLT select, but for some reason the select only finds two such nodes and cannot get their text at all. What am I doing wrong?

Result:

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

1 Answers

2
votes

You've got the arguments of contains the wrong way round, and your select is only looking at immediate children of the global-instance, not descendants deeper down.

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