2010-11-21 102 views
1

這裏是一個XML文件XML元素位置

<?xml version="1.0" encoding="UTF-8"?> 

<contact-information> 

    <full-name>Peter John</full-name> 
    <address_line_1>some place</address_line_1> 
    <address_line_2>some place2</address_line_2> 
    <city>some city</city> 
    <state>some state</state> 
    <zip>54000</zip> 
    <country>some country</country> 
    <phone>2121314144</phone> 
    <email>[email protected]</email> 

</contact-information> 

<professional-experience-section> 
    <section-name>PROFESSIONAL EXPERIENCE</section-name> 
    <enabled>true</enabled> 
    <company> 
    <name>Computer Sciences Corporation</name> 
    <city>New York</city> 
    <state>NY</state> 
    <country>United States</country> 
    <job-title> 
     <title>Senior Software Engineer</title> 
     <start-date>Aug 1996</start-date> 
     <end-date>May 2010</end-date> 
     <ongoing>false</ongoing> 
     <job-description> 
     <bullet-point> 
      <statement>C#, Visual Basic, Asp.net</statement> 
     </bullet-point> 
     <bullet-point> 
      <statement>Inspect completed work to ensure conformance to specifications, standards, and contract requirements.</statement> 
     </bullet-point> 
     <bullet-point> 
      <statement>Another Work Description.</statement> 
     </bullet-point> 
     </job-description> 
    </job-title> 
    </company> 
    <company> 
    <name>Acme</name> 
    <city>Silver Spring</city> 
    <state>MD</state> 
    <country>United States</country> 
    <job-title> 
     <title>Manager</title> 
     <start-date>Jul 2010</start-date> 
     <end-date>present</end-date> 
     <ongoing>true</ongoing> 
     <job-description> 
     <bullet-point> 
      <statement>Report to management regarding the finances of establishment.</statement> 
     </bullet-point> 
     <bullet-point> 
      <statement>Prepare, examine, or analyze accounting records, financial statements, or other financial reports to assess accuracy, completeness, and conformance to reporting and procedural standards.</statement> 
     </bullet-point> 
     <bullet-point> 
      <statement>Prepare, examine, or analyze accounting records, financial statements, or other financial reports to assess accuracy, completeness, and conformance to reporting and procedural standards.</statement> 
     </bullet-point> 
     </job-description> 
    </job-title> 

    <job-title> 
     <title>Analyst</title> 
     <start-date>Jul 2001</start-date> 
     <end-date>Jan 2010</end-date> 
     <ongoing>false</ongoing> 
     <job-description> 
     <bullet-point> 
      <statement>Study and rehearse roles from scripts in order to interpret, learn and memorize lines, stunts, and cues as directed.</statement> 
     </bullet-point> 
     <bullet-point> 
      <statement>Work closely with directors, other actors, and playwrights to find the interpretation most suited to the role.</statement> 
     </bullet-point> 
     </job-description> 
    </job-title> 

    </company> 
</professional-experience-section> 


<education-section> 
    <section-name>EDUCATION</section-name> 
    <enabled>true</enabled> 
    <institution> 
    <name>Allston Community College</name> 
    <city>Akron</city> 
    <state>MA</state> 
    <country>United States</country> 
    <degree>Bachelor of Art in Marketing Candidate</degree> 
    <end-date>Jan 2020</end-date> 
    <ongoing>true</ongoing> 
    <expected-completion-date>Jan 2020</expected-completion-date> 
    <completed></completed> 
    <bullet-point> 
     <statement>detail of what i did at the allston community college</statement> 
    </bullet-point> 
    </institution> 
    <institution> 
    <name>Boston College</name> 
    <city>Boston</city> 
    <state>MA</state> 
    <country>United States</country> 
    <degree>Bachelor of Science in Information Systems</degree> 
    <end-date>Jul 1990</end-date> 
    <ongoing>false</ongoing> 
    <expected-completion-date></expected-completion-date> 
    <completed>true</completed> 
    <bullet-point> 
     <statement>Lorem Ipsum</statement> 
    </bullet-point> 
    </institution> 
    <institution> 
    <name>Testing</name> 
    <city>Testing</city> 
    <state>PA</state> 
    <country>United States</country> 
    <degree>Completed coursework towards Bachelor of Art in Marketing</degree> 
    <end-date>Mar 1963</end-date> 
    <ongoing>false</ongoing> 
    <expected-completion-date></expected-completion-date> 
    <completed>false</completed> 
    <bullet-point> 
     <statement>Lorem Ipsum</statement> 
    </bullet-point> 
    </institution> 
    <institution> 
    <name>Testing</name> 
    <city>Testing</city> 
    <state>PA</state> 
    <country>United States</country> 
    <degree>Completed coursework towards Bachelor of Art in Marketing</degree> 
    <end-date>Mar 1963</end-date> 
    <ongoing>false</ongoing> 
    <expected-completion-date></expected-completion-date> 
    <completed>false</completed> 
    <bullet-point> 
     <statement>Lorem Ipsum</statement> 
    </bullet-point> 
    </institution> 
</education-section> 
<additional-skills-section> 
    <section-name>ADDITIONAL SKILLS</section-name> 
    <enabled>true</enabled> 
    <layout>1 Column</layout> 
    <bullet-point> 
    <statement>Additional Skills 1</statement> 
    </bullet-point> 
    </additional-skills-section> 
<custom-section> 
    <section-name>PUBLICATIONS</section-name> 
    <layout>2</layout> 
    <bullet-point> 
    <statement>test</statement> 
    </bullet-point> 
</custom-section> 

此xml文件由下面的xsl轉化:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/resume"> 
    <xsl:value-of select="/resume/contact-information/full-name"/> 
    <xsl:value-of select="/resume/contact-information/address_line_1"/> 
    <xsl:value-of select="/resume/contact-information/address_line_2"/> 
    <xsl:value-of select="/resume/contact-information/city"/> 
    <xsl:value-of select="/resume/contact-information/state"/> 
    <xsl:value-of select="/resume/contact-information/country"/> 
    <xsl:value-of select="/resume/contact-information/phone"/> 

    <xsl:for-each select="/resume/professional-experience-section/company"> 
     <!--for company name--> 
     <xsl:value-of select="name"/> 
     <xsl:value-of select="city"/>, <xsl:value-of select="state"/> 
     <xsl:value-of select="country"/> 
     <!--loop into job title--> 
     <xsl:for-each select="job-title"> 
      <!--for job title--> 
      <xsl:value-of select="title"/> 
      <!--for job start date and job end date--> 
      <xsl:value-of select="start-date"/> – <xsl:value-of 
       select="end-date"/> 
      <!--Loop into job description--> 
      <xsl:for-each select="job-description"> 
       <!--loop into each bullet point--> 
       <xsl:for-each select="bullet-point"> 
        <xsl:value-of select="statement"/> 
       </xsl:for-each> 
      </xsl:for-each> 
     </xsl:for-each> 
    </xsl:for-each> 


    <!--education section--> 
    <!--for education section's section name--> 
    <xsl:value-of select="/resume/education-section/section-name"/> 
    <!--loop into each institution name--> 
    <xsl:for-each select="/resume/education-section/institution"> 
     <!--for institution name --> 
     <xsl:value-of select="name"/> 
     <!--for institution city and state--> 
     <xsl:value-of select="city"/>, <xsl:value-of select="state"/> 
     <xsl:value-of select="country"/> 
     <!--for degree of a particular institution--> 
     <xsl:value-of select="degree"/>-<xsl:value-of select="expected-completion-date"/> 
     <xsl:for-each select="bullet-point"> 
      <xsl:for-each select="statement" > 
       <xsl:value-of select="."/> 
      </xsl:for-each> 
     </xsl:for-each> 
    </xsl:for-each> 


</xsl:template> 

專業部分將始終呈現在教育部分之前。我想開發一種機制,可以在xsl中更改部分,例如,如果我想通過使用某種輸入來顯示教育部分的信息而不是專業人員,並且我不需要手動執行操作,我應該如何更改xml或xsl文件,以便我可以在運行時更改上面列出的xsl中的每個部分的順序?

+0

好問題,+1。查看我的答案以獲得快速且完全參數化的解決方案。 :) – 2010-11-22 00:06:06

回答

0

有兩個單獨的xsl樣式表;一個如上所述,另一個與

<xsl:value-of select="/resume/education-section/section-name"/> 

循環超前職業經驗循環。

在運行時,決定要調用哪個樣式表。

這真不是意味着是一個智能自作聰明的回答,老實說,我認爲這是實現提問者想要的東西

+0

我也認爲這有多個樣式表,但假設如果我有兩個以上的部分我將不得不作出多少種組合? – 2010-11-21 21:41:00

0

下變換使用外部參數(和可以很容易地調整到最佳途徑從另一個文件中讀取)將所需的部分和他們希望的順序

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

<xsl:param name="pRanks"> 
    <section name="contact-information" r="1"/> 
    <section name="professional-experience-section" r="2"/> 
    <section name="education-section" r="3"/> 
</xsl:param> 

<xsl:variable name="vRanks" select= 
    "document('')/*/xsl:param[@name='pRanks']"/> 

<xsl:template match="/resume"> 

    <xsl:apply-templates select="*[name()=$vRanks/*/@name]"> 
    <xsl:sort select="$vRanks/*[@name=name(current())]/@r" 
      data-type="number"/> 
    </xsl:apply-templates> 
</xsl:template> 

<xsl:template match="contact-information"> 
    <xsl:value-of select="full-name"/> 
    <xsl:value-of select="address_line_1"/> 
    <xsl:value-of select="address_line_2"/> 
    <xsl:value-of select="city"/> 
    <xsl:value-of select="state"/> 
    <xsl:value-of select="country"/> 
    <xsl:value-of select="phone"/> 
</xsl:template> 

<xsl:template match="company"> 
    <!--for company name--> 
    <xsl:value-of select="name"/> 
    <xsl:value-of select="city"/>, <xsl:value-of select="state"/> 
    <xsl:value-of select="country"/> 
    <!--loop into job title--> 
    <xsl:for-each select="job-title"> 
    <!--for job title--> 
    <xsl:value-of select="title"/> 
     <!--for job start date and job end date--> 
     <xsl:value-of select="start-date"/> – <xsl:value-of 
      select="end-date"/> 
     <!--Loop into job description--> 
     <xsl:for-each select="job-description"> 
      <!--loop into each bullet point--> 
      <xsl:for-each select="bullet-point"> 
      <xsl:value-of select="statement"/> 
      </xsl:for-each> 
     </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="institution"> 
    <!--for institution name --> 
    <xsl:value-of select="name"/> 
    <!--for institution city and state--> 
    <xsl:value-of select="city"/>, <xsl:value-of select="state"/> 
    <xsl:value-of select="country"/> 
    <!--for degree of a particular institution--> 
    <xsl:value-of select="degree"/>-<xsl:value-of select="expected-completion-date"/> 
    <xsl:for-each select="bullet-point"> 
    <xsl:for-each select="statement" > 
     <xsl:value-of select="."/> 
    </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="education-section"> 
    <!--education section--> 
    <!--for education section's section name--> 
    <xsl:value-of select="section-name"/> 
    <!--loop into each institution name--> 
    <xsl:apply-templates select="institution"/> 
</xsl:template> 
</xsl:stylesheet> 

請注意

我沒有糾正了所提供的任何特定處理 - 僅將其分組爲模板並提供排序機制。由OP(原始海報)決定如何最終確定演示文稿。

特別是,大多數代碼仍然可以通過將代碼分組到模板中進行優化,這樣可以使代碼解耦,簡化代碼並解除依賴關係和硬編碼信息。

1

該樣式表:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:param name="pOrder" select="concat('contact-information|', 
              'professional-experience-section|', 
              'education-section')"/> 
    <xsl:template match="institution/completed| 
         institution/end-date| 
         institution/ongoing| 
         education-section/enable| 
         professional-experience-section 
          /*[not(self::company)]| 
         custom-section| 
         additional-skills-section| 
         contact-information/zip| 
         contact-information/email| 
         job-title/ongoing" priority="1"/> 
    <xsl:template match="text()"> 
     <xsl:value-of select="concat(.,'&#xa;')"/> 
    </xsl:template> 
    <xsl:template match="resume"> 
     <xsl:apply-templates> 
      <xsl:sort select="string-length(
           substring-before($pOrder, 
                name()))"/> 
     </xsl:apply-templates> 
    </xsl:template> 
    <xsl:template match="institution/city|company/city"> 
     <xsl:value-of select="concat(.,',')"/> 
    </xsl:template> 
    <xsl:template match="job-title/start-date"> 
     <xsl:value-of select="concat(.,' - ')"/> 
    </xsl:template> 
    <xsl:template match="institution/degree"> 
     <xsl:value-of select="concat(.,'-')"/> 
    </xsl:template> 
</xsl:stylesheet> 

利用這種正確以及形成輸入(resume根元素),輸出:

Peter John 
some place 
some place2 
some city 
some state 
some country 
2121314144 
Computer Sciences Corporation 
New York,NY 
United States 
Senior Software Engineer 
Aug 1996 - May 2010 
C#, Visual Basic, Asp.net 
Inspect completed work to ensure conformance to specifications, standards, and contract requirements. 
Another Work Description. 
Acme 
Silver Spring,MD 
United States 
Manager 
Jul 2010 - present 
Report to management regarding the finances of establishment. 
Prepare, examine, or analyze accounting records, financial statements, or other financial reports to assess accuracy, completeness, and conformance to reporting and procedural standards. 
Prepare, examine, or analyze accounting records, financial statements, or other financial reports to assess accuracy, completeness, and conformance to reporting and procedural standards. 
Analyst 
Jul 2001 - Jan 2010 
Study and rehearse roles from scripts in order to interpret, learn and memorize lines, stunts, and cues as directed. 
Work closely with directors, other actors, and playwrights to find the interpretation most suited to the role. 
EDUCATION 
true 
Allston Community College 
Akron,MA 
United States 
Bachelor of Art in Marketing Candidate-Jan 2020 
detail of what i did at the allston community college 
Boston College 
Boston,MA 
United States 
Bachelor of Science in Information Systems-Lorem Ipsum 
Testing 
Testing,PA 
United States 
Completed coursework towards Bachelor of Art in Marketing-Lorem Ipsum 
Testing 
Testing,PA 
United States 
Completed coursework towards Bachelor of Art in Marketing-Lorem Ipsum 

$pOrder參數是與一個相對順序來處理的序列resume孩子。