挑选独特的头脑

时间:2014-03-17 05:22:00

标签: xml xslt xslt-1.0

我已经按照类别排序和分组了一些XML。所以,我正在考虑使用XSLT 1.0分组来提取类别头。但我想知道是否有一种更简单的方法来简单地选择每个类别和组的第一个标题并删除或忽略重复项。

以下是XML示例:

<dataroot>
  <CaseStudies>
    <category>1</category>
    <GroupNo>2</GroupNo>
    <H1>Evaluation and Management</H1>
    <H2>Office or Other Outpatient Services</H2>
    <H3>New Patient</H3>
    <indicators>{+}</indicators>
    <code>99201</code>
    <Fulldesc>Office or other outpatient.</Fulldesc>
    <HTMLdesc>
      <b>Office or other outpatient visit.</b>
    </HTMLdesc>
    <GlobalPeriod>XXX</GlobalPeriod>
    <assist_ref>CPT Assistant Winter</assist_ref>
    <changes_ref>CPT Changes: An Insider&apos;s View 2011, 2013</changes_ref>
    <case_study>Initial office visit.</case_study>
    <pre>Review the medical history.</pre>
    <intra>Obtain a problem focused history.</intra>
    <post>Complete medical record documentation.</post>
    <tip>Levels of E/M service.</tip>
  </CaseStudies>
  <CaseStudies>
    <category>1</category>
    <GroupNo>2</GroupNo>
    <H1>Evaluation and Management</H1>
    <H2>Office or Other Outpatient Services</H2>
    <H3>Established Patient</H3>
    <indicators>{+}</indicators>
    <code>99202</code>
    <Fulldesc>Office or other outpatient visit f.</Fulldesc>
    <HTMLdesc>
      <b>Office or other outpatient visit.</b>
    </HTMLdesc>
    <GlobalPeriod>XXX</GlobalPeriod>
    <assist_ref>CPT Assistant Winter 91:11</assist_ref>
    <changes_ref>CPT Changes</changes_ref>
    <case_study>Initial office visit.</case_study>
    <pre>Review the medical history.</pre>
    <intra>Obtain an expanded problem.</intra>
    <post>Complete medical record documentation.</post>
    <tip>pending proof</tip>
  </CaseStudies>
  <CaseStudies>
    <category>1</category>
    <GroupNo>3</GroupNo>
    <H1>Anesthesia</H1>
    <H2>Intrathoracic</H2>
    <H3>New Patient</H3>
    <indicators>{+}</indicators>
    <code>99203</code>
    <Fulldesc>Office or other outpatient visit.</Fulldesc>
    <HTMLdesc>
      <b>Office or other outpatient visit.</b>
    </HTMLdesc>
    <GlobalPeriod>XXX</GlobalPeriod>
    <assist_ref>CPT Assistant Winter 91:11</assist_ref>
    <changes_ref>CPT Changes: An Insider&apos;s View 2013</changes_ref>
    <case_study>Initial office visit.</case_study>
    <pre>Review the medical history form completed by the patient and vital signs obtained by clinical staff. Communicate with other health care professionals as necessary.</pre>
    <intra>Obtain a detailed history.</intra>
    <post>Complete medical record documentation.</post>
    <tip>pending proof</tip>
  </CaseStudies>
  <CaseStudies>
    <category>1</category>
    <GroupNo>3</GroupNo>
    <H1>Anesthesia</H1>
    <H2>Intrathoracic</H2>
    <H3>Established Patient</H3>
    <indicators>{+}</indicators>
    <code>99203</code>
    <Fulldesc>Office or other outpatient visit.</Fulldesc>
    <HTMLdesc>
      <b>Office or other outpatient visit.</b>
    </HTMLdesc>
    <GlobalPeriod>XXX</GlobalPeriod>
    <assist_ref>CPT Assistant Winter 91:11</assist_ref>
    <changes_ref>CPT Changes: An Insider&apos;s View 2013</changes_ref>
    <case_study>Initial office visit.</case_study>
    <pre>Review the medical history form completed by the patient and vital signs obtained by clinical staff. Communicate with other health care professionals as necessary.</pre>
    <intra>Obtain a detailed history.</post>
    <tip>pending proof</tip>
  </CaseStudies>
</dataroot>

请注意我的样本中有h1,h2和h3头,但也可能有h4,h5和h6头。

所以,我希望按原样返回内容,但只显示每个UNIQUE HEAD h1-h6,只有在第一次出现在由h1,h2,h3等每个部分管理的每个部分中时才会显示。

H1 Surgery
  h2 Hospital Inpatient Services
    h3 Subsequent Observation Care
      h4 New Patient
      h4 Existing Patient
h1 Opthamology
  h2 Hospital Observation Services
    h3 Subsequent Observation Care
      h4 New Patient
      h4 Existing Patient
      h4 Subsequent Hospital Care
h1 Anesthesia
  h2 Hospital Observation Services
    h3 Subsequent Observation Care
      h4 New Patient
      h4 Existing Patient
      h4 Subsequent Hospital Care

这可以在不创建组和密钥的情况下完成吗?

我必须使用XSLT 1.0

2 个答案:

答案 0 :(得分:0)

这是一种使用密钥仅保留唯一标题的方法。如果标题是按以下连接分组的标题组中的第一个标题,则标题被认为是唯一的:(1)元素名称,(2)父category元素的CaseStudies子元素,以及( 3)价值。

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="h" match="H1|H2|H3|H4|H5|H6" use="concat(name(), '|', ../category, '|', .)" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="H1|H2|H3|H4|H5|H6">
    <xsl:if test="count(. | key('h', concat(name(), '|', ../category, '|', .))[1]) = 1">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

应用于输入示例的简化和更正版本时:

<dataroot>
   <CaseStudies>
      <category>1</category>
      <H1>Evaluation and Management</H1>
      <H2>Office or Other Outpatient Services</H2>
      <H3>New Patient</H3>
   </CaseStudies>
   <CaseStudies>
      <category>1</category>
      <H1>Evaluation and Management</H1>
      <H2>Office or Other Outpatient Services</H2>
      <H3>Established Patient</H3>
    </CaseStudies>
   <CaseStudies>
      <category>1</category>
      <H1>Anesthesia</H1>
      <H2>Intrathoracic</H2>
      <H3>New Patient</H3>
   </CaseStudies>
   <CaseStudies>
      <category>1</category>
      <H1>Anesthesia</H1>
      <H2>Intrathoracic</H2>
      <H3>Established Patient</H3>
   </CaseStudies>
</dataroot>

获得以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot>
   <CaseStudies>
      <category>1</category>
      <H1>Evaluation and Management</H1>
      <H2>Office or Other Outpatient Services</H2>
      <H3>New Patient</H3>
   </CaseStudies>
   <CaseStudies>
      <category>1</category>
      <H3>Established Patient</H3>
   </CaseStudies>
   <CaseStudies>
      <category>1</category>
      <H1>Anesthesia</H1>
      <H2>Intrathoracic</H2>
   </CaseStudies>
   <CaseStudies>
      <category>1</category>
   </CaseStudies>
</dataroot>

答案 1 :(得分:0)

在这种情况下,由于输入始终是合理的顺序,所以您真正需要做的就是将每个H*元素的值与前一个中相同命名元素的值进行比较 CaseStudies元素,并禁止任何相同的元素。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@*|node()" name="ident">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="H1|H2|H3|H4|H5|H6">
    <xsl:if test="not(. = ../preceding-sibling::*[1]/*[name() = name(current())])">
      <xsl:call-template name="ident" />
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

注意我使用的是not(x = y)而不是x != y,以便可能

../preceding-sibling::*[1]