如何使用XSLT从XML显示相同类型的数据

时间:2010-11-22 04:33:01

标签: xml xslt

我正在使用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的一部分,你可以看到有相同类型的两个记录,现在我在XSLT的参数中得到了<countryName>,上面的示例是我的 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>

2 个答案:

答案 0 :(得分: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文档(包装在顶部元素中以便更好):

<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>

答案 1 :(得分:0)

你可以有一个模板

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