使用XSLT对数据进行分组缺少结果

时间:2012-12-09 20:11:47

标签: xml xslt

我遇到了一些问题XML,我正在尝试从中创建可用的XML,但我的XSLT无法正常工作。我有顶部工作,但我无法将数据显示在下半部分。

以下是XML数据的示例:

<crystalreports>
<Group Level="1">
<Group Level="2">
<Group Level="3">
<Details Level="4">
<Section SectionNumber="0">
<ACPGDEGREE1>AAS</ACPGDEGREE1>
<CCD11/>
<ACPGCOMMENTS1>The Accounting program is designed to provide the theoretical and practical background necessary for supervisory and administrative careers in accounting and accounting-related areas. This degree requires a minimum of 64 credits in program requirements, program electives, and general education as listed below.</ACPGCOMMENTS1>
<ICCB1>3203</ICCB1>
<DEPARTMENT12>ACCOU</DEPARTMENT12>
<ACADPROGRAMSID1>ACCOU.AAS</ACADPROGRAMSID1>
<CRSNO1>1150</CRSNO1>
<CRSTITLE11>Managerial Accounting</CRSTITLE11>
<CRSMINCRED1>4.00</CRSMINCRED1>
<ACRBPRINTEDSPEC1/>
<ACPGHOMELANGNOTREQDRSN1>General Education - 12 to 16 (In addition to those listed above)  Total Credits Required - 64 to 70</ACPGHOMELANGNOTREQDRSN1>
</Section>
</Details>
<Details Level="4">
<Section SectionNumber="0">
<ACPGDEGREE1>AAS</ACPGDEGREE1>
<CCD11/>
<ACPGCOMMENTS1>The Accounting program is designed to provide the theoretical and practical background necessary for supervisory and administrative careers in accounting and accounting-related areas. This degree requires a minimum of 64 credits in program requirements, program electives, and general education as listed below.</ACPGCOMMENTS1>
<ICCB1>3203</ICCB1>
<DEPARTMENT12>ACCOU</DEPARTMENT12>
<ACADPROGRAMSID1>ACCOU.AAS</ACADPROGRAMSID1>
<CRSNO1>2205</CRSNO1>
<CRSTITLE11>Federal Taxation I</CRSTITLE11>
<CRSMINCRED1>3.00</CRSMINCRED1>
<ACRBPRINTEDSPEC1/>
<ACPGHOMELANGNOTREQDRSN1>General Education - 12 to 16 (In addition to those listed above)  Total Credits Required - 64 to 70</ACPGHOMELANGNOTREQDRSN1>
</Section>
</Details>
<GroupFooter>
<Section SectionNumber="0"/>
</GroupFooter>
</Group>
<GroupFooter>
<Section SectionNumber="0"/>
</GroupFooter>
</Group>
<GroupFooter>
<Section SectionNumber="0"/>
</GroupFooter>
</Group>
<Group Level="1">
<Group Level="2">
<Group Level="3">
<Details Level="4">
<Section SectionNumber="0">
<ACPGDEGREE1/>
<CCD11>ACAC</CCD11>
<ACPGCOMMENTS1>The Accounting certificate requires a minimum 32 credits in the courses listed below.    </ACPGCOMMENTS1>
<ICCB1>4207</ICCB1>
<DEPARTMENT12>ACCOU</DEPARTMENT12>
<ACADPROGRAMSID1>ACCOU.CER</ACADPROGRAMSID1>
<CRSNO1>1221</CRSNO1>
<CRSTITLE11>Introduction to Spreadsheets</CRSTITLE11>
<CRSMINCRED1>3.00</CRSMINCRED1>
<ACRBPRINTEDSPEC1/>
<ACPGHOMELANGNOTREQDRSN1/>
</Section>
</Details>
<Details Level="4">
<Section SectionNumber="0">
<ACPGDEGREE1/>
<CCD11>ACAC</CCD11>
<ACPGCOMMENTS1>The Accounting certificate requires a minimum 32 credits in the courses listed below.   </ACPGCOMMENTS1>
<ICCB1>4207</ICCB1>
<DEPARTMENT12>ACCOU</DEPARTMENT12>
<ACADPROGRAMSID1>ACCOU.CER</ACADPROGRAMSID1>
<CRSNO1>1150</CRSNO1>
<CRSTITLE11>Managerial Accounting</CRSTITLE11>
<CRSMINCRED1>4.00</CRSMINCRED1>
<ACRBPRINTEDSPEC1/>
<ACPGHOMELANGNOTREQDRSN1/>
</Section>
</Details>
<Details Level="4">
<Section SectionNumber="0">
<ACPGDEGREE1/>
<CCD11>ACAC</CCD11>
<ACPGCOMMENTS1>The Accounting certificate requires a minimum 32 credits in the courses listed below.</ACPGCOMMENTS1>
<ICCB1>4207</ICCB1>
<DEPARTMENT12>ACCOU</DEPARTMENT12>
<ACADPROGRAMSID1>ACCOU.CER</ACADPROGRAMSID1>
<CRSNO1>1175</CRSNO1>
<CRSTITLE11>Microcomputer Accounting</CRSTITLE11>
<CRSMINCRED1>2.00</CRSMINCRED1>
<ACRBPRINTEDSPEC1/>
<ACPGHOMELANGNOTREQDRSN1/>
</Section>
</Details>
<GroupFooter>
<Section SectionNumber="0"/>
</GroupFooter>
</Group>
<GroupFooter>
<Section SectionNumber="0"/>
</GroupFooter>
</Group>
<GroupFooter>
<Section SectionNumber="0"/>
</GroupFooter>
</Group>
</crystalreports>

这是XSLT:

<?xml version="1.0"?><!-- DWXMLSource="STX049 Course 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="Group/Group/Group/Details" use="Section/DEPTSDESC1" />

<xsl:template match="CrystalReport">

<crystalreports>
<xsl:for-each select="Group/Group/Group/Details[count(. | key('degrees-by-title', Section/DEPTSDESC1)[1]) = 1]">
<department>
<Degreetitle><xsl:value-of select="Section/ACPGDEGREE1"/></Degreetitle><xsl:text> DEGREE 
</xsl:text><DegreeDesc><xsl:value-of select="Section/ACPGCOMMENTS1"/></DegreeDesc><xsl:text> 
ICCB Code </xsl:text><ICCBcode><xsl:value-of select="Section/ICCB1"/></ICCBcode><xsl:text> | Field of Study     Code: </xsl:text><ProgramID><xsl:value-of select="Section/ACADPROGRAMSID1"/></ProgramID><xsl:text> 
Program Requirements
</xsl:text>
<xsl:for-each select="key('degrees-by-title', Section/DEPTSDESC1)">
<xsl:sort select="Section/DEPARTMENT12" />
<Details> 
<class><deptname><xsl:value-of select="Section/DEPARTMENT12"/></deptname><xsl:text> </xsl:text><courseno>    <xsl:value-of select="Section/CRSNO1"/></courseno><classname><xsl:value-of select="Section/CRSTITLE11"/>    </classname><xsl:text> </xsl:text><classcredit><xsl:value-of select="Section/CRSMINCRED1"/></classcredit>    </class><xsl:text>
</xsl:text> 
</Details>
</xsl:for-each>
</department><xsl:text>
</xsl:text>
</xsl:for-each>
</crystalreports>
</xsl:template>
</xsl:stylesheet>

上半部分正常工作,但我没有在底部循环中获取任何数据,也无法找出原因。

基本上,我想这样结束:

<crystalreports>
<department>
<Degreetitle>ACPGDEGREE1</Degreetitle> DEGREE 
<DegreeDesc>ACPGCOMMENTS1</DegreeDesc>
ICCB Code <ICCBcode>ICCB1</ICCBcode> | Field of Study  Code: <ProgramID>ACADPROGRAMSID1</ProgramID>

Program Requirements
<xsl:for-each select="key('degrees-by-title', Section/DEPTSDESC1)">
<xsl:sort select="Section/DEPARTMENT12" />
<Details> 
<class><deptname>DEPARTMENT12</deptname><courseno>CRSNO1</courseno><classname>CRSTITLE11</classname <classcredit>CRSMINCRED1</classcredit></class>
</Details>
</xsl:for-each>
</department>
</xsl:for-each>
</crystalreports>

1 个答案:

答案 0 :(得分:0)

假设XML样本中的多个根是OP的错误,那么解决方案就在下面。

这是错误的:

  • xsl:template match="CrystalReport"需要替换为匹配的XML元素路径(源XML中没有CrystalReport标记;
  • degrees-by-title密钥使用了不存在的节点名称。

<强>解

<?xml version="1.0"?>
<!-- DWXMLSource="STX049 Course 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="Group/Group/Group/Details" use="Section" />

  <xsl:template match="Group/Group/Group">
    <crystalreports>
      <xsl:for-each select="Details[count(. | key('degrees-by-title', Section/DEPTSDESC1)[1]) = 1]">
        <department>
          <Degreetitle>
            <xsl:value-of select="Section/ACPGDEGREE1"/>
          </Degreetitle>
          <xsl:text> DEGREE 
</xsl:text>
          <DegreeDesc>
            <xsl:value-of select="Section/ACPGCOMMENTS1"/>
          </DegreeDesc>
          <xsl:text> 
ICCB Code </xsl:text>
          <ICCBcode>
            <xsl:value-of select="Section/ICCB1"/>
          </ICCBcode>
          <xsl:text> | Field of Study     Code: </xsl:text>
          <ProgramID>
            <xsl:value-of select="Section/ACADPROGRAMSID1"/>
          </ProgramID>
          <xsl:text> 
Program Requirements
</xsl:text>
          <xsl:for-each select="key('degrees-by-title', Section)">
            <xsl:sort select="Section/DEPARTMENT12" />
            <Details>
              <class>
                <deptname>
                  <xsl:value-of select="Section/DEPARTMENT12"/>
                </deptname>
                <xsl:text> </xsl:text>
                <courseno>
                  <xsl:value-of select="Section/CRSNO1"/>
                </courseno>
                <classname>
                  <xsl:value-of select="Section/CRSTITLE11"/>
                </classname>
                <xsl:text> </xsl:text>
                <classcredit>
                  <xsl:value-of select="Section/CRSMINCRED1"/>
                </classcredit>
              </class>
              <xsl:text>
</xsl:text>
            </Details>
          </xsl:for-each>
        </department>
        <xsl:text>
</xsl:text>
      </xsl:for-each>
    </crystalreports>
  </xsl:template>
</xsl:stylesheet>