2012-12-11 82 views
0

我已經讓我的XSLT工作得很好,但是我遇到了一些問題。XSLT不生成根元素

首先,它不產生ROOT元素。

其次,XML數據有兩個元素<ACPGDEGREE1><CCD11>。當一個包含數據時,另一個是空的,反之亦然。如果<ACPGDEGREE1>包含數據,我想插入數據並在其後面添加文本「DEGREE」。如果<CCD11>包含數據,我想忽略數據但插入文本「CERTIFICATE」。

任何幫助將不勝感激。

這裏是XSLT:

<?xml version="1.0"?> 
<!-- DWXMLSource="STX049 Course Catalog Parsed_FINAL.xml" --> 
<!DOCTYPE xsl:stylesheet> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="xml" indent="no"/> 

    <xsl:key name="degrees-by-title" match="Section" use="ICCB1"/> 

    <xsl:template match="CrystalReport/Group"> 

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

    </xsl:template> 

</xsl:stylesheet> 

下面是示例XML:

<?xml version="1.0" encoding="UTF-8"?> 
<CrystalReport> 
    <Group Level="3"> 
    <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> 
    <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> 
    <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>2241</CRSNO1> 
     <CRSTITLE11>Intermediate Accounting I</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> 
    <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>2242</CRSNO1> 
     <CRSTITLE11>Intermediate Accounting II</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> 
    <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>2251</CRSNO1> 
     <CRSTITLE11>Cost Accounting</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> 
    <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>1100</CRSNO1> 
     <CRSTITLE11>Introduction to Business</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> 
    <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>2201</CRSNO1> 
     <CRSTITLE11>Macroeconomics and the Global Economy</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> 
    <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>1140</CRSNO1> 
     <CRSTITLE11>Financial 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> 
    <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>1114</CRSNO1> 
     <CRSTITLE11>Business Ethics</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> 
    <GroupFooter> 
     <Section SectionNumber="0"/> 
    </GroupFooter> 
    </Group> 
</CrystalReport> 
+0

請張貼XML實時樣本一次..這不僅保持業務機密還幫助其他成員瞭解您的要求並做出相應的迴應。 –

+0

如果可能,請發佈sample-compact XML和期望的輸出XML(手 - 編碼肯定)!這將引導你更快地解決其他成員.. –

+1

這是一個較小的樣本。但我會盡量讓他們在未來變小。 –

回答

1

之所以沒有根元素是,你還沒有寫,將產生的任何代碼。正常的方法是有一個模板匹配的源文件的根目錄例如

<xsl:template match="/"> 
    <root> 
    <xsl:apply-templates/> 
    </root> 
</xsl:template> 

的要求本聲明:

If <ACPGDEGREE1> contains data, I want to insert the data and add the text "DEGREE" after it. If <CCD11> contains data, I want to ignore the data but insert the text "CERTIFICATE". 

似乎很容易映射到兩個模板規則:

<xsl:template match="ACPGDEGREE1[child::node()]"> 
    <xsl:value-of select="."/> 
    <xsl:text> DEGREE</xsl:text> 
</xsl:template> 

<xsl:template match="CCD11[child::node()]"> 
    <xsl:text> CERTIFICATE</xsl:text> 
</xsl:template> 

你已經研究出如何用一組規則來表達你的需求(當我看到這個時,我想這樣做),但是你似乎並沒有把握XSLT編碼在很大程度上是c這些規則直接將這些規則轉化爲模板規則。

+0

你是對的。我完全理解XSLT有一個問題。你知道一本好書或者教程會有幫助嗎? –

+2

是的 - 我寫了一個。搜索「XSLT 2.0程序員參考」 –

+0

@JimMaivald,Michael Kay是最好的作家之一!檢查這個帖子你會發現很多參考! http://stackoverflow.com/questions/1858345/xsltwhich-is-the-best-tutorial-you-would-like-to-recommend –