不同的价值观XSLT

时间:2014-10-03 20:20:46

标签: xslt

我正在尝试使用xml中提供的属性链接来自XSLT的未排序列表项。

我想创建一个将生成:

的输出
<li><a href="department/CHEM.html">Chemistry and Chemical Biology</a></li>

该链接将指向部门代码属性。

MY XSL

<xsl:template match="/">
    <xsl:for-each select="distinct-values(//courses/course/department/dept_short_name)">
      <li><object><xsl:value-of select="."/></object></li>
    </xsl:for-each>
</xsl:template>

XML

<courses>
   <course acad_year="2014" cat_num="5085" offered="Y">
      <term term_pattern_code="1" fall_term="Y" spring_term="N">fall term</term>
      <department code="CHEM">
         <dept_long_name>Department of Chemistry and Chemical Biology</dept_long_name>
         <dept_short_name>Chemistry and Chemical Biology</dept_short_name>
      </department>
      <course_group code="CHEM">Chemistry</course_group>
      <course_number>
         <num_int>17</num_int>
      </course_number>
      <title>Principles of Organic Chemistry</title>
      <course_type>Lecture and Lab</course_type>
      <course_level code="P">Primarily for Undergraduates</course_level>
      <credit code="1">Half course</credit>
      <meeting_text>M., W., 2:30 - 4:00 pm (*Friday - optional lecture); and a weekly, ninety-minute section to be arranged</meeting_text>
      <faculty_text>Eric N. Jacobsen</faculty_text>
      <description>An introduction to organic chemistry, with an emphasis on structure and bonding, reaction mechanisms, and chemical reactivity.</description>
      <prerequisites>Open to freshmen with a score of 750 or higher in the College Boards or the Chemistry Placement Examination; to students who scored 4 or 5 on the Chemistry Advanced Placement Examination; and to students who achieved a grade of B or higher in either Physical Sciences 1, 10, 11, or another college-level introductory chemistry course. Others may enter only by permission of the instructor.</prerequisites>
      <notes>The Chemistry 17/27 sequence is intended primarily for students in the life sciences, who have completed LS1a and one of the PS courses (PS1, PS10, PS11). The Chemistry 20/30 sequence is intended primarily for students planning a concentration in Chemistry or the physical sciences. Either sequence satisfies the organic chemistry requirement for medical school. Students may not count both Chemistry 17 and Chemistry 20 for degree credit. On the other hand, Chemistry 27 and Chemistry 30 cover different material, so students may choose to take both courses for degree credit; students should ordinarily take the third half course only after completing either the 17/27 or 20/30 sequence.</notes>
      <instructor_approval_required>N</instructor_approval_required>
      <faculty_list>
         <faculty term="1" role="H" id="F9726b7dc2454aadb1d8243ac0cbfb81a">
            <name>
               <prefix/>
               <first>Eric</first>
               <middle>N.</middle>
               <last>Jacobsen</last>
               <suffix/>
            </name>
         </faculty>
      </faculty_list>
      <schedule>
         <meeting term="1" optional="N" type="Lecture" day="1" begin_time="1430" end_time="1600"/>
         <meeting term="1" optional="N" type="Lecture" day="3" begin_time="1430" end_time="1600"/>
         <meeting term="1" optional="Y" type="Lecture" day="5" begin_time="1430" end_time="1600"/>
      </schedule>
      <meeting_locations>
         <location term="1" building="Science Center" room=" C" type="Lecture"/>
      </meeting_locations>
      <requirements>
         <requirement type="Core Curriculum" name="Science A"/>
      </requirements>
   </course>
   <course acad_year="2014" cat_num="876" offered="Y">
      <term term_pattern_code="2" fall_term="N" spring_term="Y">spring term</term>
      <department code="CHEM">
         <dept_long_name>Department of Chemistry and Chemical Biology</dept_long_name>
         <dept_short_name>Chemistry and Chemical Biology</dept_short_name>
      </department>
      <course_group code="CHEM">Chemistry</course_group>
      <course_number>
         <num_int>20</num_int>
      </course_number>
      <title>Organic Chemistry</title>
      <course_type>Lecture and Lab</course_type>
      <course_level code="P">Primarily for Undergraduates</course_level>
      <credit code="1">Half course</credit>
      <meeting_text>M., W., F., at 9; weekly lecture review F. 2-3:30; one-hour weekly discussion section; five lab experiments each consisting of a lab-oriented lecture and 5 hours hands-on</meeting_text>
      <faculty_text>Ryan M. Spoering</faculty_text>
      <description>An introduction to structure and bonding in organic molecules; mechanisms of organic reactions; chemical transformations of the functional groups of organic chemistry; synthesis; determination of chemical structures by infrared and NMR spectroscopy.</description>
      <prerequisites>Open to students who scored 4 or 5 on the Chemistry Advanced Placement Examination, or who successfully completed Life Sciences 1A or Life and Physical Sciences A. Others should contact the instructor to discuss their preparation.</prerequisites>
      <notes>Chemistry 20/30 is an integrated two-semester sequence that prepares students to study chemistry and other physical sciences, whereas the Chemistry 17/27 sequence focuses on application of organic chemistry concepts to the life sciences. Either sequence satisfies the organic chemistry requirement for medical school and the chemistry concentration. The content of Chemistry 17 is accelerated and overlaps with topics from both Chemistry 20 and 30. Students may not count both Chemistry 17 and 20 toward the degree. However, Chemistry 27 and Chemistry 20/30 cover different material, so students may choose to take Chemistry 27 after completing the 20/30 sequence.</notes>
      <instructor_approval_required>N</instructor_approval_required>
      <faculty_list>
         <faculty term="2" role="H" id="F1b5cf8d5ad23c4bf67595e2f5c8899b6">
            <name>
               <prefix/>
               <first>Ryan</first>
               <middle>Michael</middle>
               <last>Spoering</last>
               <suffix/>
            </name>
         </faculty>
      </faculty_list>
      <schedule>
         <meeting term="2" optional="N" type="Lecture" day="1" begin_time="900" end_time="1000"/>
         <meeting term="2" optional="N" type="Lecture" day="3" begin_time="900" end_time="1000"/>
         <meeting term="2" optional="N" type="Lecture" day="5" begin_time="900" end_time="1000"/>
      </schedule>
      <meeting_locations/>
      <requirements>
         <requirement type="Core Curriculum" name="Science A"/>
      </requirements>
   </course>
</course>

2 个答案:

答案 0 :(得分:0)

不是使用distinct-values,而是使用xsl:for-each-group代替

<xsl:for-each-group select="//courses/course/department" group-by="dept_short_name">

然后,您可以使用Attribute Value Template设置属性,如下所示:

<a href="department/{@code}.html">

试试这个XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/">
        <xsl:for-each-group select="//courses/course/department" group-by="dept_short_name">
          <li><a href="department/{@code}.html"><xsl:value-of select="current-grouping-key()"/></a></li>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

答案 1 :(得分:0)

这是你可以看到山雀的另一种方式:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>

<xsl:key name="dept-by-code" match="department" use="@code" />
<xsl:variable name="doc" select="document(base-uri())" />

<xsl:template match="/">
    <xsl:for-each select="distinct-values(courses/course/department/@code)">
        <li>
            <a href="department/{.}.html">
                <xsl:value-of select="key('dept-by-code', ., $doc)[1]/dept_short_name" />
            </a>
        </li>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

应用于格式正确,最小化的输入示例:

<courses>
   <course>
      <department code="CHEM">
         <dept_short_name>Chemistry and Chemical Biology</dept_short_name>
      </department>
   </course>
   <course>
      <department code="ABC">
         <dept_short_name>Atomic and Biological Chemistry</dept_short_name>
      </department>
   </course>
   <course>
      <department code="CHEM">
         <dept_short_name>Chemistry and Chemical Biology</dept_short_name>
      </department>
   </course>
   <course>
      <department code="DEF">
         <dept_short_name>Dept. of Ecological Fragmentation</dept_short_name>
      </department>
   </course>
</courses>

结果:

<li>
   <a href="department/CHEM.html">Chemistry and Chemical Biology</a>
</li>
<li>
   <a href="department/ABC.html">Atomic and Biological Chemistry</a>
</li>
<li>
   <a href="department/DEF.html">Dept. of Ecological Fragmentation</a>
</li>