【问题标题】:How to find all nested XML nodes with certain attribute value using XSLT如何使用 XSLT 查找具有特定属性值的所有嵌套 XML 节点
【发布时间】:2014-01-29 17:52:49
【问题描述】:

我有多层深层 XML(对于长示例非常抱歉),其中包含错误消息的各个级别的节点。这些节点都具有以“_error_message”结尾的属性@id。我正在尝试使用 XSLT 选择将所有这些错误收集到一个平面列表中,但由于某种原因,选择只能找到两个这样的节点并且根本无法获取它们的文本。我做错了什么?

结果:

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

【问题讨论】:

    标签: xml xslt xslt-1.0


    【解决方案1】:

    你把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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2023-04-02
      • 1970-01-01
      • 2015-05-18
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      相关资源
      最近更新 更多