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

クエリ文字列を前処理できる場合は、パーツをパラメーターとして変換に渡すことができます。

設定したパラメーターに基づいて、それはできないと思います。そのため、以下のコードは、渡された 1 つのパラメーター (URL) を解析して、各パラメーターを取得します。私が使用した方法は、クエリ文字列への 1 つの変更を想定していることに注意してください。それは、アンパサンドで終わることができるということです。できない場合は、もう少し洗練された文字列解析方法が必要になります。FunctX には、動作するはずの substring-before-if-contains があります。そうしないと、クエリの最後の項目が空の文字列になります。

次の XSL は、サンプルの URL スニペットを url パラメーターとして (最後のアンパサンドを追加して) 渡して実行すると、次の 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>

お役に立てば幸いです。

于 2012-12-05T20:28:00.267 に答える