我有多级深度XML(非常抱歉长篇例),各级别的节点包含错误消息。这些节点都具有以“_error_message”结尾的属性@id。我试图通过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>
答案 0 :(得分:2)
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>