【问题标题】:Xslt transformation for grouping fromatting用于分组格式的 Xslt 转换
【发布时间】: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


【解决方案1】:

我需要应用条件来检查&lt;DocumentList&gt; 节点是否存在,如果不存在,则显示“未找到数据”。

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

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

【讨论】:

  • 我是个新手,你能帮我详细说明你想说的话吗:)
  • @umangyadav 我不明白那个评论。你看过代码吗?你试过了吗?
  • 我试过了,总是找不到数据
  • 你能修改我的代码并告诉我你的意思吗,我无法理解你的意思
  • 我明白你的意思,谢谢我是新手,所以花了一些时间,谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
相关资源
最近更新 更多