Home > Net >  Xslt transformation for grouping fromatting
Xslt transformation for grouping fromatting

Time:10-25

This is the XML I want to traverse, grouping the data by <SUBJECT>. I am already able to do that, but I need to apply a condition to check if a <DocumentList> node is there, and if it's not there, show "no data found". Also with this it is taking also data that I don't want, like the screenshot below:

<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 I tried:

<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 that I want similar to with grouping for same data:

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

CodePudding user response:

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

Use <xsl:apply-templates> and two different templates. One matches <KnowledgeBaseAdvancedSearchResponse> that has <DocumentList> (and <Document>), and the other matches all other cases.

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