【问题标题】:Add Group title to three-level XSLT grouping将组标题添加到三级 XSLT 分组
【发布时间】:2013-02-05 19:55:14
【问题描述】:

下面的 XSLT 完成了我需要的所有事情,我可以按学位然后按选修课和要求对我的 XML 数据进行分组。但是我不能让<ACPGAREAOFSTUDY1> 标题在每个部分中只出现一次。我需要一个在学位之外的父分组,只有一个 <ACPGAREAOFSTUDY1> 数据元素来介绍它。

这是 XSLT:

 <?xml version="1.0"?><!-- DWXMLSource="STX049 Catalog parsed.xml" -->
 <!DOCTYPE xsl:stylesheet>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="no"/>
 <xsl:key name="degrees-by-title" match="Section" use="ICCB1" />

 <xsl:template match="/*">
 <CrystalReport>
  <xsl:apply-templates select="Group/Group/Group"/>
 </CrystalReport>
 </xsl:template>

 <xsl:template match="Group">
 <xsl:text>
 </xsl:text><Degree><areaofstudy><xsl:value-of select="ACPGAREAOFSTUDY1" /></areaofstudy>
 <xsl:apply-templates select="Details/Section[generate-id() = generate-id(key('degrees-by-title', ICCB1)[1])]" mode="group">
 <xsl:sort select="ACADPROGRAMSID1" />
 </xsl:apply-templates>
 </Degree>
 </xsl:template>

 <xsl:template match="Section" mode="group">
 <department><Degreetitle><xsl:apply-templates select="ACPGDEGREE1" /></Degreetitle>
 <Certtitle><xsl:apply-templates select="CCD11" /></Certtitle><xsl:text>
 </xsl:text><DegreeDesc><xsl:apply-templates select="ACPGCOMMENTS1" /></DegreeDesc><xsl:text>
 ICCB Code </xsl:text><ICCBcode><xsl:apply-templates select="ICCB1" /></ICCBcode><xsl:text> | Field of Study Code: </xsl:text><ProgramID><xsl:apply-templates select="ACADPROGRAMSID1" /></ProgramID>
 <xsl:variable name="courses" select="key('degrees-by-title', ICCB1)" />

 <xsl:call-template name="CourseGroup">
 <xsl:with-param name="courses" select="$courses[FlagElectives1 = 'N']" />
 <xsl:with-param name="title" select="'Program Requirements'" />
 </xsl:call-template>

 <xsl:call-template name="CourseGroup">
 <xsl:with-param name="courses" select="$courses[FlagElectives1 = 'Y']" />
 <xsl:with-param name="title" select="'Program Electives'" />
 </xsl:call-template>
 </department>
 </xsl:template>

 <xsl:template name="CourseGroup">
 <xsl:param name="courses" />
 <xsl:param name="title" />

 <xsl:if test="$courses"><xsl:text>
 </xsl:text><xsl:value-of select="$title" />
 <xsl:apply-templates select="$courses">
 <xsl:sort select="FlagElectives1" order="ascending" />
 <xsl:sort select="DEPARTMENT11" />
 <xsl:sort select="CRSNO1" />
 </xsl:apply-templates>
 </xsl:if>
 </xsl:template>

 <xsl:template match="Section"><xsl:text>
 </xsl:text><Details><class><deptname><xsl:apply-templates select="DEPARTMENT11" /></deptname><xsl:text>    </xsl:text><courseno><xsl:apply-templates select="CRSNO1" /></courseno><xsl:text>   </xsl:text>
 <classname><xsl:apply-templates select="CRSTITLE1" /></classname><xsl:text>    </xsl:text>
 <classcredit><xsl:apply-templates select="CRSMINCRED1" /></classcredit></class></Details>
 </xsl:template>

 <xsl:template match="ACPGDEGREE1/text()">
 <xsl:value-of select="concat(., ' DEGREE')"/>
 </xsl:template>

 <xsl:template match="CCD11/text()">
 <xsl:value-of select="concat(., ' CERTIFICATE')" />
 </xsl:template>
 </xsl:stylesheet>

这是基本的 XML 结构:

<?xml version="1.0" encoding="UTF-8"?>
 <CrystalReport>
 <Group Level="1">
  <Group Level="2">
  <Group Level="3">
    <Details>
      <Section>
        <ACPGDEGREE1>AAS</ACPGDEGREE1>
        <CCD11/>
        <ACPGCOMMENTS1>The Accounting program</ACPGCOMMENTS1>
        <ICCB1>3203</ICCB1>
        <ACADPROGRAMSID1>ACCOU.AAS</ACADPROGRAMSID1>
        <CRSNO1>1110</CRSNO1>
        <ACRBPRINTEDSPEC1/>
        <ACPGHOMELANGNOTREQDRSN1>General Education</ACPGHOMELANGNOTREQDRSN1>
        <CRSMINCRED1>2</CRSMINCRED1>
        <ACPGAREAOFSTUDY1>Accounting</ACPGAREAOFSTUDY1>
        <CRSTITLE1>Using Computers: An Introduction</CRSTITLE1>
        <DEPARTMENT11>ACCOU</DEPARTMENT11>
        <CRSSUBJECT1>CIS</CRSSUBJECT1>
        <ACRBLABEL1>CIS REQUIREMENT</ACRBLABEL1>
        <CRSMAXCRED1/>
        <FlagElectives1>N</FlagElectives1>
      </Section>
    </Details>
    <Details>
      <Section>
        <ACPGDEGREE1>AAS</ACPGDEGREE1>
        <CCD11/>
        <ACPGCOMMENTS1>The Accounting program</ACPGCOMMENTS1>
        <ICCB1>3203</ICCB1>
        <ACADPROGRAMSID1>ACCOU.AAS</ACADPROGRAMSID1>
        <CRSNO1>1150</CRSNO1>
        <ACRBPRINTEDSPEC1/>
        <ACPGHOMELANGNOTREQDRSN1>General Education</ACPGHOMELANGNOTREQDRSN1>
        <CRSMINCRED1>3</CRSMINCRED1>
        <ACPGAREAOFSTUDY1>Accounting</ACPGAREAOFSTUDY1>
        <CRSTITLE1>Intro to Computer</CRSTITLE1>
        <DEPARTMENT11>ACCOU</DEPARTMENT11>
        <CRSSUBJECT1>CIS</CRSSUBJECT1>
        <ACRBLABEL1>CIS</ACRBLABEL1>
        <CRSMAXCRED1/>
        <FlagElectives1>Y</FlagElectives1>
      </Section>
    </Details>
  </Group>
  </Group>
 </Group>
</CrystalReport>

生成以下 XML:

<CrystalReport>
 <Degrees><areaofstudy>Accounting</areaofstudy)
 <Degree>
<department>
  <Degreetitle>AAS DEGREE</Degreetitle>
  <Certtitle />
  <DegreeDesc>The Accounting program</DegreeDesc>
  <ICCBcode>3203</ICCBcode>
  <ProgramID>ACCOU.AAS</ProgramID>
  <h1>Program Requirements</h1>
  <Details>
    <class>
      <deptname>ACCOU</deptname>
      <courseno>1150</courseno>
      <classname>Intro to Computer</classname>
      <classcredit>3</classcredit>
      <Elective>Y</Elective>
    </class>
  </Details>
  <h1>Program Electives</h1>
  <Details>
    <class>
      <deptname>ACCOU</deptname>
      <courseno>1110</courseno>
      <classname>Using Computers: An Introduction</classname>
      <classcredit>2</classcredit>
      <Elective>N</Elective>
    </class>
  </Details>
 </department>
</Degree>
</Degrees>
</CrystalReport>

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    怎么样:

    <?xml version="1.0"?>
    <!-- DWXMLSource="STX049 Catalog parsed.xml" -->
    <!DOCTYPE xsl:stylesheet>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="no"/>
      <xsl:variable name="allSections"
                    select="/CrystalReport/Group/Group/Group/Details/Section" />
    
      <xsl:key name="kArea" match="Section" use="ACPGAREAOFSTUDY1"/>
      <xsl:key name="kDepartment" match="Section"
               use="concat(ACPGAREAOFSTUDY1, '+', ICCB1)" />
    
      <xsl:variable name="deptFirsts"
                 select="$allSections[generate-id() = 
                   generate-id(key('kDepartment',
                    concat(ACPGAREAOFSTUDY1, '+', ICCB1))[1])]" />
    
    
      <xsl:template match="/">
        <CrystalReport>
          <Degrees>
            <xsl:apply-templates
              select="$allSections[generate-id() = 
                     generate-id(key('kArea', ACPGAREAOFSTUDY1)[1])]"
              mode="group"/>
          </Degrees>
        </CrystalReport>
      </xsl:template>
    
      <xsl:template match="Section" mode="group">
        <xsl:variable name="area" select="ACPGAREAOFSTUDY1" />
        <areaofstudy>
          <xsl:value-of select="$area"/>
        </areaofstudy>
        <xsl:text>&#xA;</xsl:text>
        <Degree>
          <xsl:apply-templates select="$deptFirsts[ACPGAREAOFSTUDY1 = $area]"
                               mode="department">
            <xsl:sort select="ACADPROGRAMSID1" />
          </xsl:apply-templates>
        </Degree>
      </xsl:template>
    
      <xsl:template match="Section" mode="department">
        <department>
          <xsl:apply-templates select="." mode="departmentValues" />
    
          <xsl:variable name="courses"
                  select="key('kDepartment', 
                       concat(ACPGAREAOFSTUDY1, '+', ICCB1))" />
    
          <xsl:call-template name="CourseGroup">
            <xsl:with-param name="courses" select="$courses[FlagElectives1 = 'N']" />
            <xsl:with-param name="title" select="'Program Requirements'" />
          </xsl:call-template>
    
          <xsl:call-template name="CourseGroup">
            <xsl:with-param name="courses" select="$courses[FlagElectives1 = 'Y']" />
            <xsl:with-param name="title" select="'Program Electives'" />
          </xsl:call-template>
        </department>
      </xsl:template>
    
      <xsl:template match="Section" mode="departmentValues">
        <Degreetitle>
          <xsl:apply-templates select="ACPGDEGREE1" />
        </Degreetitle>
        <Certtitle>
          <xsl:apply-templates select="CCD11" />
        </Certtitle>
        <xsl:text>&#xA;</xsl:text>
        <DegreeDesc>
          <xsl:apply-templates select="ACPGCOMMENTS1" />
        </DegreeDesc>
        <xsl:text>&#xA;ICCB Code</xsl:text>
        <ICCBcode>
          <xsl:apply-templates select="ICCB1" />
        </ICCBcode>
        <xsl:text> | Field of Study Code: </xsl:text>
        <ProgramID>
          <xsl:apply-templates select="ACADPROGRAMSID1" />
        </ProgramID>
      </xsl:template>
    
      <xsl:template name="CourseGroup">
        <xsl:param name="courses" />
        <xsl:param name="title" />
    
        <xsl:if test="$courses">
          <xsl:text>&#xA;</xsl:text>
          <xsl:value-of select="$title" />
          <xsl:apply-templates select="$courses">
            <xsl:sort select="FlagElectives1" order="ascending" />
            <xsl:sort select="DEPARTMENT11" />
            <xsl:sort select="CRSNO1" />
          </xsl:apply-templates>
        </xsl:if>
      </xsl:template>
    
      <xsl:template match="Section">
        <xsl:text>&#xA;</xsl:text>
        <Details>
          <class>
            <deptname>
              <xsl:apply-templates select="DEPARTMENT11" />
            </deptname>
            <xsl:text>    </xsl:text>
            <courseno>
              <xsl:apply-templates select="CRSNO1" />
            </courseno>
            <xsl:text>   </xsl:text>
            <classname>
              <xsl:apply-templates select="CRSTITLE1" />
            </classname>
            <xsl:text>    </xsl:text>
            <classcredit>
              <xsl:apply-templates select="CRSMINCRED1" />
            </classcredit>
          </class>
        </Details>
      </xsl:template>
    
      <xsl:template match="ACPGDEGREE1/text()">
        <xsl:value-of select="concat(., ' DEGREE')"/>
      </xsl:template>
    
      <xsl:template match="CCD11/text()">
        <xsl:value-of select="concat(., ' CERTIFICATE')" />
      </xsl:template>
    </xsl:stylesheet>
    

    在您的示例 XML 上运行时,会产生:

    <CrystalReport>
      <Degrees>
        <areaofstudy>Accounting</areaofstudy>
        <Degree>
          <department>
            <Degreetitle>AAS DEGREE</Degreetitle><Certtitle />
            <DegreeDesc>The Accounting program</DegreeDesc>
            ICCB Code<ICCBcode>3203</ICCBcode> | Field of Study Code: <ProgramID>ACCOU.AAS</ProgramID>
            Program Requirements
            <Details>
              <class>
                <deptname>ACCOU</deptname>
                <courseno>1110</courseno>
                <classname>Using Computers: An Introduction</classname>
                <classcredit>2</classcredit>
              </class>
            </Details>
            Program Electives
            <Details>
              <class>
                <deptname>ACCOU</deptname>
                <courseno>1150</courseno>
                <classname>Intro to Computer</classname>
                <classcredit>3</classcredit>
              </class>
            </Details>
          </department>
        </Degree>
      </Degrees>
    </CrystalReport>
    

    【讨论】:

    • 通过 Dreamweaver + Safari 和 Oxygen 运行后,我只得到 标题,没有其他数据,例如 会计文科暖气、空调和空调制冷...
    • 我查看了代码以查看是否有任何错误的换行符或损坏的元素,但没有找到。但我只是重新进行了转换,它现在似乎可以在 Dreamweaver/Safari 组合中使用。在氧气中,我不断收到记忆错误。知道为什么吗?
    • 请注意,此 XSLT 似乎比您建议的其他版本占用更多内存。
    • 我明白了。如果您有一个非常大的输入文档,可能是由于使用了 3 个键,但我目前想不出另一种方法。
    • 这个 XSLT 中有一个巨大的错误。它正在为每个 1 级标题重新创建所有 2 级数据。它将所有课程的完整副本放在每个标题下。这可能就是为什么在 Oxygen 中生成和给出内存输出错误需要这么长时间的原因。
    猜你喜欢
    • 2022-08-20
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 2023-03-06
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多