2012-12-09 36 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,應該只出現一次。然後,我們需要該程序中的類的列表。然後,轉到下一個學位。 –