2010-11-22 45 views
1

我正在使用XSLT和XML。如何使用XSLT顯示來自XML的相同類型的數據

我有下面的XML。

<documentCountryInformation> 
     <countryCode>US</countryCode> 
     <countryName>United States</countryName> 
     <sufficientDocumentation>Conditional</sufficientDocumentation> 
     <sectionInformation> 
      <sectionName>Health</sectionName> 
      <documentParagraph paragraphId="23628"> 
      <paragraphType>Requirement</paragraphType> 
      <paragraphText> 
       <p> 
       Vaccination for 
       <strong>yellow fever</strong> 
       Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 6 days. 
       </p> 
      </paragraphText> 
      </documentParagraph>    
     </sectionInformation> 
</documentCountryInformation> 
<documentCountryInformation> 
     <countryCode>IN</countryCode> 
     <countryName>India</countryName> 
     <sufficientDocumentation>Conditional</sufficientDocumentation> 
     <sectionInformation> 
      <sectionName>Health</sectionName> 
      <documentParagraph paragraphId="23648"> 
      <paragraphType>Requirement</paragraphType> 
      <paragraphText> 
       <p> 
       Vaccination for 
       <strong>Dengue fever</strong> 
       Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 3 days. 
       </p> 
      </paragraphText> 
      </documentParagraph>    
     </sectionInformation> 
</documentCountryInformation> 

以上是完整的XML的一部分,你可以看到有2個記錄同一類型的,現在我已經在上面的例子中XSLT的參數得到了<countryName>國家名稱參數將包含這種類型的數據「美國,印度」,現在我想拆分參數數據,並進一步它將檢查具有相同國家名稱和顯示數據的XML,我的意思是將有循環國名和以下是所需的HTML。

<div class="resultsContainer" id="divTransit"> 
     <h3>Transit - United States (US)</h3>        
     <p> 
     Vaccination for 
     <strong>yellow fever</strong> 
     Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 6 days. 
     </p> 

     <h3>Transit - India (IN)</h3>        
     <p> 
     Vaccination for 
     <strong>Dengue fever</strong> 
     Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 3 days. 
     </p> 
</div> 
+0

我不明白的問題。這個輸出對於XSLT模板匹配看起來很直接,爲什麼你需要一個「循環」? – 2010-11-22 05:14:28

+0

它是我的想法是的你是對的,我們可以使用模板匹配,我想我們可能需要,因爲我也分裂了參數值。請建議 – 2010-11-22 05:26:11

+0

好問題,+1。查看我的答案,獲取完整而簡短的解決方案。 :) – 2010-11-22 05:35:48

回答

1

現在我要拆分的參數數據 並進一步將調查具有相同的國名和顯示 根據數據,我的意思是對國家名稱會有 是循環,下面是XML 需要的HTML。

沒有必要「分裂」的參數值,也沒有對任何一種的「循環」。

該轉化

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:param name="pCountryName" select="'United States,India'"/> 

<xsl:template match="/*"> 
    <div class="resultsContainer" id="divTransit"> 
    <xsl:apply-templates select= 
     "*[contains(concat(',',$pCountryName,','), 
        concat(',',countryName,',') 
       ) 
     ] 
     "/> 
    </div> 
</xsl:template> 

<xsl:template match="documentCountryInformation"> 
    <h3> 
    <xsl:value-of select= 
    "concat('Transit - ', 
      countryName, 
      ' (', 
      countryCode, 
      ')' 
      ) 
    "/> 
    </h3> 
    <xsl:copy-of select="*/*/paragraphText/node()"/> 
</xsl:template> 
</xsl:stylesheet> 

當所提供的XML文檔施加(包裹在頂部元件成爲簡潔(wellformed)):

<t> 
    <documentCountryInformation> 
     <countryCode>US</countryCode> 
     <countryName>United States</countryName> 
     <sufficientDocumentation>Conditional</sufficientDocumentation> 
     <sectionInformation> 
      <sectionName>Health</sectionName> 
      <documentParagraph paragraphId="23628"> 
       <paragraphType>Requirement</paragraphType> 
       <paragraphText> 
        <p> 
       Vaccination for 
         <strong>yellow fever</strong> 
       Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 6 days. 
        </p> 
       </paragraphText> 
      </documentParagraph> 
     </sectionInformation> 
    </documentCountryInformation> 
    <documentCountryInformation> 
     <countryCode>IN</countryCode> 
     <countryName>India</countryName> 
     <sufficientDocumentation>Conditional</sufficientDocumentation> 
     <sectionInformation> 
      <sectionName>Health</sectionName> 
      <documentParagraph paragraphId="23648"> 
       <paragraphType>Requirement</paragraphType> 
       <paragraphText> 
        <p> 
       Vaccination for 
         <strong>Dengue fever</strong> 
       Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 3 days. 
        </p> 
       </paragraphText> 
      </documentParagraph> 
     </sectionInformation> 
    </documentCountryInformation> 
</t> 

產生想要的,正確的結果

<div class="resultsContainer" id="divTransit"> 
    <h3>Transit - United States (US)</h3> 

    <p> 
       Vaccination for 
         <strong>yellow fever</strong> 
       Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 6 days. 
        </p> 

    <h3>Transit - India (IN)</h3> 

    <p> 
       Vaccination for 
         <strong>Dengue fever</strong> 
       Persons without valid yellow fever certification, if required, are subject to quarantine for a period up to 3 days. 
        </p> 

</div> 
0

你可以有一個模板

<xsl:template match="documentCountryInformation[contains($countryName, countryName)]"> 
+0

請注意,假定沒有國家名稱可以包含任何其他國家/地區名稱。如果你想更強大的匹配,你可以追加分隔符,就像@ Dimitre的答案所示。 – LarsH 2010-11-22 12:30:57

相關問題