首页 > 解决方案 > 在 XSL `for-each` 循环中编辑出部分 XML 文件

问题描述

我正在尝试在 XSLfor-each循环中编辑出部分 XML 文件。

我有一个带有多个模板的 XML 文件,而 XSL 文件有一个 for-each 循环来显示所有模板。

我的目标是通过定位节点内的单个 templateId 来消除在 XSL for-each 循环中显示的某些模板。

这是代码:

XML

<component typeCode="SEWS" contextConductionInd="true">
<section>
<templateId root="2.12.840.1.103883.13.20.23.2.68"/>
<code code="58907-0" codeSystem="1.33.890.1.176583.6.1" codeSystemName="GTRFC" displayName="Scenic Reports"/>
<title>Scenic Reports</title>
<text>
<table border="1" width="100%">
<thead>
<tr>
<th>Report</th>
<th>Value</th>
<th>Date</th>
<th>Source</th>
</tr>
</thead>
<tbody>
<tr ID="GREETER_1">
<td ID="GREETER_1">Display Output1</td>
<td ID="GREETER_1">
<list>
<item>test data<br/>
</item>
</list>
</td>
<td>06/12/2019</td>
<td>Location TBD</td>
</tr>
</tbody>
</table>
</text>
<entry typeCode="FRED" contextConductionInd="true">
<act classCode="FRE" moodCode="FRED">
<templateId root="2.12.840.1.103883.13.20.23.2.69" extension="2019-5-3"/>
<code code="34109-9" codeSystem="1.33.890.1.176583.6.2" codeSystemName="DEERS" displayName="FRED">
<!--Code must match or be equivalent to section code -->
<translation code="57898-0" codeSystem="1.33.890.1.176983.6.2" codeSystemName="DEERT" displayName="Scenic Reports"/>
</code>
<text>
<reference value="#DEERS_0"/>
</text>
<statusCode code="completed"/>
<effectiveTime value="547654653325.000"/>
<author>
<time value="4356754356.000"/>
<assignedAuthor>
<id nullFlavor="NA"/>
<addr nullFlavor="NA"/>
<telecom nullFlavor="NA"/>
<assignedPerson>
<name>Location TBD</name>
</assignedPerson>
<representedOrganization>
<id nullFlavor="NA"/>
<name>Location TBD</name>
<telecom nullFlavor="NA"/>
<addr nullFlavor="NA"/>
</representedOrganization>
</assignedAuthor>
</author>
</act>
</entry>
</section>
</component>

XSL

<xsl:template name="section">
<xsl:call-template name="section-title">
<xsl:with-param name="title" select="n1:title"/>
</xsl:call-template>
<xsl:call-template name="section-author"/>
<xsl:call-template name="section-text"/>
<xsl:for-each select="n1:component/n1:section">
<xsl:call-template name="nestedSection">
<xsl:with-param name="margin" select="2"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>

我想知道我们是否能够在 XSL for-each 循环中编辑 XML 文档中的内容,基于<templateId root="1.1.5.33.4.33"...><code code="87654-1"...>.

我已经尝试<xsl:if><xsl:choose><xsl:when>声明无济于事。

标签: xmltemplatesxsltforeach

解决方案


你也许可以尝试这样的事情:

<xsl:for-each select="templateId">
  <xsl:if test="not(@root='1.1.5.33.4.33' and @root='...somethingElse...')">
    ... your existing code ...
  </xsl:if>
</xsl:for-each>

或者干脆

<xsl:for-each select="templateId[not(@root='1.1.5.33.4.33' and @root='...somethingElse...')]">
  ... your existing code ...
</xsl:for-each>

推荐阅读