【发布时间】:2021-12-10 03:12:15
【问题描述】:
这是我要遍历的 XML,将数据按<SUBJECT> 分组。我已经能够做到这一点,但我需要应用一个条件来检查 <DocumentList> 节点是否存在,如果不存在,则显示“未找到数据”。此外,它还获取了我不想要的数据,如下面的屏幕截图:
<KnowledgeBase>
<DocumentCount>8</DocumentCount>
<CountOnly>false</CountOnly>
<DocumentList>
<Document Identifier="428B474B-C016-4726-9325-20BC8B754427">
<SUBJECT>Bariatric Surgery Coding Guidelines</SUBJECT>
</Document>
<Document Identifier="261489E7-14E0-43CF-9909-6892A84D4BEA">
<SUBJECT>Bariatric Surgery Coding Guidelines</SUBJECT>
</Document>
<Document Identifier="1C336836-A5BB-424F-8A43-9BDD52A5BE9D">
<SUBJECT>Bariatric Surgery Coverage R2</SUBJECT>
</Document>
<Document Identifier="65E77B48-E88B-4AAF-B0A6-ED14BD028905">
<SUBJECT>Billing and Coding: Bariatric Surgery Coverage</SUBJECT>
</Document>
</DocumentList>
</KnowledgeBaseAdvancedSearchResponse>
XSLT 我试过了:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:key name="groups" match="//KnowledgeBaseAdvancedSearchResponse/DocumentList/Document" use="SUBJECT" />
<xsl:template match="//KnowledgeBaseAdvancedSearchResponse/DocumentList">
<xsl:apply-templates select="Document[generate-id() = generate-id(key('groups', SUBJECT)[1])]" />
</xsl:template>
<xsl:template match="Document">
<h1><xsl:value-of select="SUBJECT" /></h1>
</xsl:template>
</xsl:stylesheet>
我想要类似于对相同数据进行分组的 XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match="/">
<xsl:choose>
<xsl:when test="//KnowledgeBaseAdvancedSearchResponse/DocumentList">
<xsl:for-each select="//DocumentList/Document">
<h1><xsl:value-of select="SUBJECT" /></h1>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>No policy edits for the selected Payor/State.</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="//Errors">There were errors.</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
【问题讨论】:
-
下次您发布 XSLT 问题时,请不要在此处转储您的 整个 XML 和 整个 XSLT。删掉与你提出的问题无关的所有内容。你不能指望人们通读数百行不相关的代码。这次我给你做了,下次发帖前请自己做。
-
感谢您的回答和格式化,请记住
标签: xml xslt xslt-1.0 xslt-grouping