【问题标题】:edit out portions of a XML file inside an XSL `for-each` loop在 XSL `for-each` 循环中编辑出部分 XML 文件
【发布时间】:2020-10-01 01:30:03
【问题描述】:

我正在尝试编辑 XSL for-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>

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

我尝试了&lt;xsl:if&gt;&lt;xsl:choose&gt;&lt;xsl:when&gt; 语句无济于事。

【问题讨论】:

  • 请显示您需要的输出。此外,如果它是缩进的,阅读你的 XML 也会容易得多。
  • 不仅缩进,而且仅仅从样式表中提供一个模板,也令人困惑——例如,我们看不到名为nestedSectionsectionTitlesectionAuthor的被调用模板。另外,提供的 XSLT 代码片段中没有 xsl:ifxsl:choose 指令吗? ???请提供完整的 XSLT 样式表、您希望从转换中获得的完整结果、您得到的实际结果以及实际结果有什么问题。
  • 这对我来说已经很明显了,如果你同意,请告诉我,但过滤掉特定模板的唯一方法是按节点过滤,如果所有模板共享同一个节点 (templateId),你无法根据 templateId 隐藏特定模板,即
  • @matt1966 您当然可以使用节点名称和包含其 id 的谓词或您想要的任何其他字段来过滤掉您想要的内容。如果您发布您想要的完整示例,则更有可能获得适当的答案。
  • 我有一个包含多个模板的巨大 XML 文件,但我只想在 XSL 输出中显示其中的一些。 XSL 有一个现有的 for-each 循环并显示所有模板。我想根据唯一的 templateId () 过滤掉 XSL 输出中的一些模板...

标签: xml templates xslt foreach


【解决方案1】:

你可以试试这样的:

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

【讨论】:

  • 在另一个 中使用 是否可以接受?
  • @matt1966 是的,你可以这样做。
猜你喜欢
  • 2012-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多