我正在开发一个XSL样式表,其中学生按此顺序分组:
升入大学> LEVEL->&部 - GT; chair->浓度 - >学生
所以基本上结构将是"组内的组#34;如果以下内容有意义,请告诉我。如果您需要澄清,请告诉我们!
这是我尝试转换的原始XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dataroot>
<student>
<name_sort>Doe, John</name_sort>
<name>John Doe</name>
<level>Undergrad</level>
<concentration>Studio Art</concentration>
<college>College 1</college>
<department>Department 1</department>
<chair>Chair Name</chair>
</student>
<student>
<name_sort>James, Lisa</name_sort>
<name>Lisa James</name>
<level>Undergrad</level>
<concentration>Studio Art</concentration>
<college>College 1</college>
<department>Department 1</department>
<chair>Chair Name</chair>
</student>
<dataroot>
使用应用的XSL样式表的XML输出:
<Root>
<!-- Group by College -->
<college>College 1</college>
<!-- Group by Level -->
<level>Undergraduate</level>
<!-- Group by Department -->
<department>Department 1</department>
<!-- Group by Chair -->
<chair>Chair Name</chair>
<!-- Group by Concentration -->
<concentration>Studio Art</concentration>
<!-- List Students with matching concentration under parent groups -->
<student>
<name>John Doe</name>
</student>
<student>
<name>Lisa James</name>
</student>
<!-- Repeat loop -->
</Root>
这是我的尝试,但学生没有正确分组。有些学生是非学生和#34;出现在&#34;毕业生&#34;并且一些集中组被复制......
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Keys for grouping -->
<xsl:key name="colleges" match="student" use="college"/>
<xsl:key name="levels" match="student" use="level"/>
<xsl:key name="departments" match="student" use="department"/>
<xsl:key name="chairs" match="student" use="chair"/>
<xsl:key name="concentrations" match="student" use="concentration"/>
<xsl:template match="/dataroot">
<Root>
<xsl:for-each select="student[generate-id() = generate-id(key('colleges', college)[1])]">
<xsl:sort select="college" order="ascending"/>
<xsl:for-each select="key('colleges', college)[generate-id() = generate-id(key('levels', level)[1])]">
<xsl:sort select="level" order="ascending"/>
<college><xsl:value-of select="college"/></college>
<xsl:text>
</xsl:text>
<level><xsl:value-of select="level"/> degree recipients</level>
<xsl:text>
</xsl:text>
<xsl:for-each select="key('levels', level)[generate-id() = generate-id(key('departments', department)[1])]">
<xsl:sort select="department" order="ascending"/>
<department><xsl:value-of select="department"/></department>
<xsl:text>
</xsl:text>
<xsl:for-each select="key('departments', department)[generate-id() = generate-id(key('chairs', chair)[1])]">
<xsl:sort select="chair" order="ascending"/>
<chair><xsl:value-of select="chair"/></chair>
<xsl:text>
</xsl:text>
<xsl:for-each select="key('chairs', chair)[generate-id() = generate-id(key('concentrations', concentration)[1])]">
<xsl:sort select="concentration" order="ascending"/>
<concentration><xsl:value-of select="concentration"/></concentration>
<xsl:text>
</xsl:text>
<xsl:for-each select="key('concentrations', concentration)">
<xsl:sort select="name_sort" order="ascending"/>
<student>
<name><xsl:value-of select="name"/></name>
<xsl:text>
</xsl:text>
</student>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</Root>
</xsl:template>
任何人都可以帮助我吗?
答案 0 :(得分:2)
使用XSLT 2或3以xsl:for-each-group
方式嵌套xsl:for-each
很容易,但是使用XSLT 1和Muenchian分组,你需要让第二级密钥包含第一级密钥用例如
<xsl:key name="colleges" match="student" use="college"/>
<xsl:key name="levels" match="student" use="concat(college, '|', level)"/>
并使用例如
<xsl:for-each select="key('colleges', college)[generate-id() = generate-id(key('levels', concat(college, '|', level))[1])]">
然后你需要使用与第三级密钥相同的方法
<xsl:key name="departments" match="student" use="concat(department, '|', college, '|', level)"/>
和
<xsl:for-each select="key('levels', concat(college, '|', level))[generate-id() = generate-id(key('departments', concat(department, '|', college, '|', level))[1])]">
等等。
答案 1 :(得分:1)
考虑使用连接所有所需父组的单个键。无需多个嵌套for:each
次调用。在<xsl:apply-templates>
内运行所需的排序。但是,如果您有许多分组,则需要将分组容器元素作为子根。下面使用带有调整后的XML的<students>
进行演示。
<强> XML 强>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dataroot>
<student>
<name_sort>Doe, John</name_sort>
<name>John Doe</name>
<level>Undergrad</level>
<concentration>Studio Art</concentration>
<college>College 1</college>
<department>Department 1</department>
<chair>Chair Name</chair>
</student>
<student>
<name_sort>James, Lisa</name_sort>
<name>Lisa James</name>
<level>Undergrad</level>
<concentration>Economics</concentration>
<college>College 1</college>
<department>Department 1</department>
<chair>Chair Name</chair>
</student>
<student>
<name_sort>Doe, Jane</name_sort>
<name>Jane Doe</name>
<level>Undergrad</level>
<concentration>Economics</concentration>
<college>College 1</college>
<department>Department 1</department>
<chair>Chair Name</chair>
</student>
</dataroot>
<强> XSLT 强>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<!-- single key for grouping -->
<xsl:key name="concatkey" match="student" use="concat(college, level, department, chair, concentration)"/>
<xsl:template match="/dataroot">
<root>
<xsl:apply-templates select="student[generate-id() = generate-id(key('concatkey',
concat(college, level, department, chair, concentration)))]"/>
</root>
</xsl:template>
<xsl:template match="student">
<students>
<xsl:copy-of select="college"/>
<xsl:copy-of select="level"/>
<xsl:copy-of select="department"/>
<xsl:copy-of select="chair"/>
<xsl:copy-of select="concentration"/>
<xsl:for-each select="key('concatkey', concat(college, level, department, chair, concentration))">
<student><xsl:copy-of select="name"/></student>
</xsl:for-each>
</students>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<root>
<students>
<college>College 1</college>
<level>Undergrad</level>
<department>Department 1</department>
<chair>Chair Name</chair>
<concentration>Economics</concentration>
<student>
<name>Lisa James</name>
</student>
<student>
<name>Jane Doe</name>
</student>
</students>
<students>
<college>College 1</college>
<level>Undergrad</level>
<department>Department 1</department>
<chair>Chair Name</chair>
<concentration>Studio Art</concentration>
<student>
<name>John Doe</name>
</student>
</students>
</root>