2012-11-25 20 views
0

問題:根據查詢字符串傳遞的參數創建一個新的XML

我想創建根據查詢字符串傳遞的參數一個新的XML(過濾XML)。

URL示例:search_advanced.xhtml部門= CHEM &提供= Y &水平= P

例如,如果上面的查詢字符串傳遞,我會想過濾的XML只顯示那些課程?包含

  • 甲部門中等於CHEM(fas_courses /當然/部門/ @代碼)
  • 一種提供代碼等於Y(fas_course /當然/ @提供)
  • A Level課程代碼等於P(fas_course /場/ @提供)

原始的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> 

回答

0

如果可以預處理查詢字符串,可以通過部分作爲參數傳遞給轉型。

根據你設置的參數,我假設你不能這樣做,所以下面的代碼解析一個傳入的參數(URL)來派生每個參數。請注意,我使用的方法假設您的查詢字符串發生了一個變化:它可以以&符號結束。如果它不能,你需要一個更復雜的字符串解析方法。 FunctX有一個應該工作的:substring-before-if-contains。否則,查詢中的最後一項將以空字符串結尾。

以URL作爲URL參數(添加了最後一個&符號)傳入的示例URL代碼段運行時,以下XSL在以下XML(僅返回cat number 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> 

希望這是有幫助的。

相關問題