2012-12-09 38 views
0

我遇到了一些问题XML,我正在尝试从中创建可用的XML,但是我的XSLT无法正常工作。我有顶部的部分工作,但我无法让数据出现在下半部分。使用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> 
+0

您的XML示例中有多个根是故意的? –

+0

对不起,忘记了主根。我编辑它。 –

回答

0

假设重根的XML样本中OP的错误,解决的办法是下面。

这里的什么是错的:

  • xsl:template match="CrystalReport"需要用一个XML元素路径替换匹配(没有CrystalReport标签源XML;
  • degrees-by-title键使用一个不存在的节点名。

SOLUTION

<?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> 
+0

这不会返回所需的结果。对于每个类别,重复选择=“Section/ACPGCOMMENTS1”/>的'

+0

我刚刚注意到这个项目又出现了一个皱纹。 XML''和''中有两个元素。这些列出了提供的学位。当一个人填充时,另一个是空白的。 XSLT需要根据这些元素来区分结果。 –

+0

基本上每个学位,如AAC,应该只出现一次。然后,我们需要该程序中的类的列表。然后,转到下一个学位。 –