Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 使用XSLT的三级组_Xml_Xslt - Fatal编程技术网

Xml 使用XSLT的三级组

Xml 使用XSLT的三级组,xml,xslt,Xml,Xslt,我有一个XML文件,列出了学位课程下的课程。我已经创建了一个XSLT(在帮助下),它成功地创建了学位名称下的类组。但是客户需要第三个级别来识别选修课和非选修课。选修字段列出的数据为“Y”,如果是选修字段,则为“N”,否则为“课程要求” 我需要向XSLT添加另一个级别,如果元素中有一个“N”,则将类分组到标题“Program Requirement”下 我想我需要基于这个元素创建另一个键,对吧,然后创建一个模板,创建一个xsl:text标题,上面写着:“程序要求”或“程序选择”,但我被卡住了。我以

我有一个XML文件,列出了学位课程下的课程。我已经创建了一个XSLT(在帮助下),它成功地创建了学位名称下的类组。但是客户需要第三个级别来识别选修课和非选修课。选修字段列出的数据为“Y”,如果是选修字段,则为“N”,否则为“课程要求”

我需要向XSLT添加另一个级别,如果元素
中有一个“N”,则将类分组到标题“Program Requirement”下

我想我需要基于这个元素创建另一个键,对吧,然后创建一个模板,创建一个xsl:text标题,上面写着:“程序要求”或“程序选择”,但我被卡住了。我以前从未做过三级分组

以下是我的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:template match="/">

    <CrystalReport>
      <xsl:apply-templates/>
    </CrystalReport>
  </xsl:template>


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

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

    <Degree>
      <xsl:for-each select="Details[count(. | key('degrees-by-title', Section/ICCB1)[1]) = 1]">
        <xsl:sort select="Section/ACADPROGRAMSID1" />
        <department>
          <Degreetitle>
            <xsl:apply-templates select="Section/ACPGDEGREE1" />
          </Degreetitle>
          <Certtitle>
            <xsl:apply-templates select="Section/CCD11" />
          </Certtitle>
          <DegreeDesc>
            <xsl:value-of select="Section/ACPGCOMMENTS1"/>
          </DegreeDesc>
          <ICCBcode>
            <xsl:value-of select="Section/ICCB1"/>
          </ICCBcode>
          <ProgramID>
            <xsl:value-of select="Section/ACADPROGRAMSID1"/>
          </ProgramID>
          <xsl:for-each select="key('degrees-by-title', Section/ICCB1)">
            <xsl:sort select="Section/FlagElectives1" order="ascending" />
            <xsl:sort select="Section/DEPARTMENT11" />
            <xsl:sort select="Section/CRSNO1" />

            <Details>
              <class>
                <deptname>
                  <xsl:value-of select="Section/DEPARTMENT11"/>
                </deptname>
                <courseno>
                  <xsl:value-of select="Section/CRSNO1"/>
                </courseno>
                <classname>
                  <xsl:value-of select="Section/CRSTITLE1"/>
                </classname>
                <classcredit>
                  <xsl:value-of select="Section/CRSMINCRED1"/>
                </classcredit>
                <Elective>
                  <xsl:value-of select="Section/FlagElectives1" />
                </Elective>
              </class>
            </Details>

          </xsl:for-each>
        </department>
      </xsl:for-each>
    </Degree>

  </xsl:template>

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

  <xsl:template match="Section/CCD11[child::node()]">
    <xsl:text> CERTIFICATE</xsl:text>
  </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>

原子吸收光谱
会计程序
3203
会计
1110
通识教育
2.
会计
使用电脑:简介
雅库
顺式
CIS要求
N
原子吸收光谱
会计程序
3203
会计
1150
通识教育
3.
会计
计算机入门
雅库
顺式
顺式
Y
第一级分组应基于元素
。各种学位和证书的标题可在
所有这些中的第二级唯一字段是

以下是所需的输出:

<CrystalReport>
 <Degrees>

 <!--group and repeat "Degrees" for-each based on element ACPGAREAOFSTUDY1-->

 <areaofstudy>Accounting</areaofstudy>
 <Degree>
 <department>

 <!--group and repeat "department" for-each based on element ICCB1-->

  <Degreetitle>AAS DEGREE</Degreetitle>
  <Certtitle />
  <DegreeDesc>The Accounting program</DegreeDesc>
  <ICCBcode>3203</ICCBcode>
  <ProgramID>ACCOU.AAS</ProgramID>

 <!--group and repeat "Details" for-each based on element ACADPROGRAMSID1 under titles "Program Requirement" or "Program Elective" based on element "FlagElectives1"-->

  <h1>Program Requirements</h1>
  <Details>
    <class>
      <deptname>ACCOU</deptname>
      <courseno>1150</courseno>
      <classname>Intro to Computer</classname>
      <classcredit>3</classcredit>
      <Elective>N</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>Y</Elective>
    </class>
  </Details>
 </department>
 </Degree>
 <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>

会计
原子吸收度
会计程序
3203
会计
课程要求
雅库
1150
计算机入门
3.
N
课程选修课
雅库
1110
使用电脑:简介
2.
Y
原子吸收度
会计程序
3203
会计
课程要求
雅库
1150
计算机入门
3.
Y
课程选修课
雅库
1110
使用电脑:简介
2.
N

我对您的XSLT做了一些清理。任何时候,如果每个都有一个
嵌套在每个
中,这都是一个很好的迹象,表明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:variable name="allSections" 
                select="/CrystalReport/Group/Group/Group/Details/Section" />

  <xsl:key name="kArea" match="Section" use="ACPGAREAOFSTUDY1"/>
  <xsl:key name="kDegree" match="Section" 
           use="concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1)" />
  <xsl:key name="kDepartment" match="Section" 
           use="concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1, '+', ICCB1)" />

  <xsl:variable name="degreeFirsts"
           select="$allSections[generate-id() = 
             generate-id(key('kDegree', 
                concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1))[1])]" />
  <xsl:variable name="deptFirsts"
             select="$allSections[generate-id() = 
               generate-id(key('kDepartment',
                concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1, '+', 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:apply-templates select="$degreeFirsts[ACPGAREAOFSTUDY1 = $area]" 
                         mode="degree"/>
  </xsl:template>

  <xsl:template match="Section" mode="degree">
    <xsl:variable name="area" select="ACPGAREAOFSTUDY1" />
    <xsl:variable name="degree" select="ACPGDEGREE1" />
    <xsl:text>&#xA;</xsl:text>
    <Degree>
      <xsl:apply-templates
        select="$deptFirsts[ACPGAREAOFSTUDY1 = $area and ACPGDEGREE1 = $degree]"
        mode="department">
        <xsl:sort select="ACADPROGRAMSID1" />
      </xsl:apply-templates>
    </Degree>
  </xsl:template>

  <xsl:template match="Section" mode="department">
    <department>
      <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:variable name="courses" 
              select="key('kDepartment', 
                   concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1, '+', 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>&#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>
在示例输入上运行时,此XSLT将生成:

<CrystalReport>
  <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>
</CrystalReport>

原子吸收度
会计程序
3203
会计
课程要求
雅库
1150
计算机入门
3.
Y
课程选修课
雅库
1110
使用电脑:简介
2.
N

注意
h1
元素,注意要求和选修组的开始。如果您希望以其他方式表示,请澄清。

您能给我们一个示例,说明您希望输出是什么样子的吗?我看到您已经为后续问题打开了一个单独的问题。我的回答(关于名称空间)对您有帮助吗?如果是的话,请您将它们标记为答案。您是否将另一个问题的答案更新为相同的代码?如果你这样做,我就把它标为答案。我提出了第二个问题,因为有些人不允许在问题中有多个步骤。一个问题->一个答案。但是更新后的XSLT更接近最终的解决方案。现在更新了,是的,我认为打开新的问题是一个好主意,而不是让一个原始问题拖出大量的新需求。对不起,我现在完全糊涂了。我以前从未用过这种方法。使用此样式表时,我会丢失所有现有的标记名。您的样式表提供了数据,但没有返回许多项的标记。当我在数据上运行它时,我得到的是Accu2200所得税申报表准备3Y而不是:Accu2200所得税申报表准备3Y如何在结果中返回标记名?我还想使用一些元素。我是否构建了更多的模板?您是在输入中使用了我所有的XSLT,还是只是其中的一部分?我做了一个修改,通过查找旧元素名->新元素名,使XSLT更加简洁,但这使用了
document()
函数,并且XSLT处理器可能对此函数有限制。您能告诉我您使用的是什么处理器吗?我相信我使用的是您在帖子中显示的所有XSLT。我看到了97行代码。我正在通过AdobeDreamweaver和Safari运行它。这很奇怪。它在FireFox中似乎运行良好(我没有Dreamweaver或Safari)。我对它进行了修改,以使用更直接的appr
<CrystalReport>
  <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>
</CrystalReport>