2010-11-22 47 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

相关问题