用于分组格式化的 Xslt 转换

Xslt transformation for grouping fromatting

这是我要遍历的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>

I need to apply a condition to check if a <DocumentList> node is there, and if it's not there, show "no data found".

使用<xsl:apply-templates>和两个不同的模板。一个匹配具有 <DocumentList>(和 <Document>)的 <KnowledgeBaseAdvancedSearchResponse>,另一个匹配所有其他情况。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html" />

  <xsl:key name="groups" match="Document" use="SUBJECT" />

  <xsl:template match="/">
    <xsl:apply-templates select="KnowledgeBaseAdvancedSearchResponse" />
  </xsl:template>

  <xsl:template match="KnowledgeBaseAdvancedSearchResponse[DocumentList/Document]">
    <xsl:apply-templates select="DocumentList/Document[generate-id() = generate-id(key('groups', SUBJECT))]" />
  </xsl:template>
  
  <xsl:template match="KnowledgeBaseAdvancedSearchResponse">
    <h1>No Data Found</h1>
  </xsl:template>

  <xsl:template match="Document">
    <h1><xsl:value-of select="SUBJECT" /></h1>
  </xsl:template>
</xsl:stylesheet>