根据在查询字符串上传递的参数创建新的XML

时间:2012-11-25 22:51:22

标签: xslt-2.0

问题:

我正在尝试创建一个新的XML(过滤的XML),具体取决于在查询字符串上传递的参数。

网址示例:search_advanced.xhtml?department = CHEM& offers = Y& level = P

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

的课程
  • 一个与CHEM相同的部门(fas_courses / course / department / @ code)
  • 提供的代码等于Y(fas_course / course / @ offers)
  • 等级为P的等级代码(fas_course / course / @ offers)

我正在处理的原始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>

1 个答案:

答案 0 :(得分:0)

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

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

以下XSL在使用您的样本URL片段作为url param(添加了最终的&符号)运行时,在以下XML上提供了所需的结果(仅返回cat编号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>

希望这有帮助。