0

问题:

我正在尝试根据查询字符串上传递的参数创建一个新的 XML(过滤的 XML)。

URL 示例:search_advanced.xhtml?department=CHEM&offered=Y&level=P

例如,如果上面的查询字符串被传递,我希望过滤后的 XML 只显示那些包含

  • 等于 CHEM 的部门 (fas_courses/course/department/@code)
  • 提供的代码等于 Y (fas_course/course/@offered)
  • 一个级别代码等于 P (fas_course/course/@offered)

下面是我一直在处理的原始 XML 文件和 XSLT 文件。感谢您的任何建议。

原始 XML

<fas_courses>
    <course acad_year="2012" cat_num="85749" offered="N" next_year_offered="2013">
        <term term_pattern_code="4" fall_term="Y" spring_term="Y">full year</term>
        <department code="VES">
            <dept_long_name>Department of Visual and Environmental Studies</dept_long_name>
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="VES">Visual and Environmental Studies</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
        <course_type>Studio</course_type>
        <course_level code="G">Graduate Course</course_level>
        <description>A graduate workshop for Film Study Center non-fiction film and video projects.</description>
    </course>
    <course>
        .....
    </course>
    <course>
        .....
    </course>
</fas_courses>

XSL 文件

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
   <xsl:param name="url"/>
   <xsl:param name="querystring"/>
   <xsl:param name="baselink"/>
   <xsl:param name="department" select="'All'"/>
   <xsl:param name="course_group" select="'All'"/>
   <xsl:param name="description" select="'All'"/>
   <xsl:param name="level" select="'All'"/>
   <xsl:param name="term" select="'All'"/>
   <xsl:param name="offered" select="'All'"/>

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


   <xsl:template match="//course
      [
      ($department = '' or $department = 'All' or department/@code = $department) 
      and 
      ($course_group = '' or $course_group = 'All' or course_group/@code = $course_group)
      and 
      ($description = '' or $description = 'All' or description = $description)
      and 
      ($level = '' or $level = 'All' or course_level/@code = $level)
      and 
      ($term = '' or $term = 'All' or term/@term_pattern_code = $term)  
      and 
      ($offered = '' or $offered = 'All' or @offered = $offered)
      ]">
      <xsl:copy-of select="."/>    
   </xsl:template>  
</xsl:stylesheet>
4

1 回答 1

0

如果您可以预处理查询字符串,则可以将这些部分作为参数传递给转换。

根据您设置的参数,我假设您不能这样做,因此下面的代码解析一个传入的参数(URL)以派生每个参数。请注意,我使用的方法假设您的查询字符串有一个更改:它可以以 & 符号结尾。如果不能,您将需要一个稍微复杂的字符串解析方法。FunctX 有一个应该可以工作的:substring-before-if-contains。否则,查询中的最后一项将作为空字符串结束。

当使用作为 url 参数传入的示例 URL 片段(添加了最后的 & 符号)运行时,以下 XSL 在以下 XML 上提供所需的结果(仅返回目录号 85753)。

注意最后的空课程模板,它允许跳过不匹配的课程。没有它,您将看到那些不匹配课程的文本节点。

XML:

    <fas_courses>
    <course acad_year="2012" cat_num="85749" offered="N" next_year_offered="2013">
        <term term_pattern_code="4" fall_term="Y" spring_term="Y">full year</term>
        <department code="CHEM">
            <dept_long_name>Department of Visual and Environmental Studies</dept_long_name>
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="VES">Visual and Environmental Studies</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
        <course_type>Studio</course_type>
        <course_level code="G">Graduate Course</course_level>
        <description>A graduate workshop for Film Study Center non-fiction film and video projects.</description>
    </course>
    <course acad_year="2012" cat_num="85751" offered="Y" next_year_offered="2013">
        <term term_pattern_code="4" fall_term="Y" spring_term="Y">full year</term>
        <department code="CHEM">
            <dept_long_name>Department of Visual and Environmental Studies</dept_long_name>
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="VES">Visual and Environmental Studies</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
        <course_type>Studio</course_type>
        <course_level code="G">Graduate Course</course_level>
        <description>A graduate workshop for Film Study Center non-fiction film and video projects.</description>
    </course>
    <course acad_year="2012" cat_num="85753" offered="Y" next_year_offered="2013">
        <term term_pattern_code="4" fall_term="Y" spring_term="Y">full year</term>
        <department code="CHEM">
            <dept_long_name>Department of Visual and Environmental Studies</dept_long_name>
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="VES">Visual and Environmental Studies</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
        <course_type>Studio</course_type>
        <course_level code="P">Graduate Course</course_level>
        <description>A graduate workshop for Film Study Center non-fiction film and video projects.</description>
    </course>
    <course acad_year="2012" cat_num="85755" offered="N" next_year_offered="2013">
        <term term_pattern_code="4" fall_term="Y" spring_term="Y">full year</term>
        <department code="CHEM">
            <dept_long_name>Department of Visual and Environmental Studies</dept_long_name>
            <dept_short_name>Visual and Environmental Studies</dept_short_name>
        </department>
        <course_group code="VES">Visual and Environmental Studies</course_group>
        <title>Critical Media Practice: Non Fiction Filmmaking Workshop</title>
        <course_type>Studio</course_type>
        <course_level code="G">Graduate Course</course_level>
        <description>A graduate workshop for Film Study Center non-fiction film and video projects.</description>
    </course>
</fas_courses>

XSL:

<?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
   <xsl:param name="url"/>
   <xsl:param name="querystring" select="substring-after($url, '?')"/>
   <xsl:param name="baselink" select="substring-before($url, '?')"/>
   <xsl:param name="department" select="substring-before(substring-after($querystring, 'department='), '&amp;')"/>
   <xsl:param name="course_group" select="substring-before(substring-after($querystring, 'course_group='), '&amp;')"/>
   <xsl:param name="description" select="substring-before(substring-after($querystring, 'description='), '&amp;')"/>
   <xsl:param name="level" select="substring-before(substring-after($querystring, 'level='), '&amp;')"/>
   <xsl:param name="term" select="substring-before(substring-after($querystring, 'term='), '&amp;')"/>
   <xsl:param name="offered" select="substring-before(substring-after($querystring, 'offered='), '&amp;')"/>

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

    <xsl:template match="fas_courses">
        <xsl:copy>
             <xsl:apply-templates select="course"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="course
      [
      ($department = '' or $department = 'All' or department/@code = $department) 
      and 
      ($course_group = '' or $course_group = 'All' or course_group/@code = $course_group)
      and 
      ($description = '' or $description = 'All' or description = $description)
      and 
      ($level = '' or $level = 'All' or course_level/@code = $level)
      and 
      ($term = '' or $term = 'All' or term/@term_pattern_code = $term)  
      and 
      ($offered = '' or $offered = 'All' or @offered = $offered)
      ]">
        <xsl:copy-of select="."/>    
      </xsl:template>
      <xsl:template match="course"/> 
    </xsl:stylesheet>

希望这会有所帮助。

于 2012-12-05T20:28:00.267 回答