2016-09-27 34 views
1

这是我的输入XML:如何在XSLT中应用多个模板?

<root xmlns:og="http://www.example.com" xmlns:dc="http://www.example.com" > 
     <hits> 
      <hits> 
       <_index>indexname</_index> 
       <_type>indextype</_type> 
       <_source> 
        <keywords>keywords-NOT-PROVIDED</keywords> 
        <secureFlag>false</secureFlag> 
        <description>How do you make sure your search and big data applications are well-maintained? How to free IT staff&apos;s time to focus more on business goals? Watch our expert address the top 5 managed services questions asked by customers.</description> 
        <connectorSpecific> 
         <discoveredBy>http://www.example.com/enterprise-search-case-studies</discoveredBy> 
         <xslt>false</xslt> 
         <pathFromSeed>LL</pathFromSeed> 
         <md5>WVBM5NDACLWRN4OSXRCQUA5RJA</md5> 
        </connectorSpecific> 
        <title>Do I Need Managed Services for My Search and Big Data Applications?</title> 
        <url>http://www.example.com/big-data-search-managed-services-questions</url> 
        <UD>http://www.example.com/big-data-search-managed-services-questions</UD> 
        <size>21467</size> 
        <displayurl>http://www.example.com/big-data-search-managed-services-questions</displayurl> 
        <UE>http://www.example.com/big-data-search-managed-services-questions</UE> 
        <submitTime>2016-07-21T22:55:00+0000</submitTime> 
        <newContent>Site Map Terms, privacy and cookie</newContent> 
    <T>Do I Need Managed Services for My Search and Big Data Applications?</T> 
        <sourceType>heritrix</sourceType><U>http://www.example.com/big-data-search-managed-services-questions</U> 
        <sourceName>PIB</sourceName> 
        </_source> 
       <_id>http://www.example.com/big-data-search-managed-services-questions</_id> 
       <_score>1</_score> 
      </hits> 
    <hits> 
    </root> 

这是我运用XSLT:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
    <xsl:output method="xml" encoding="UTF-8" indent="yes" /> 
    <xsl:template match="/"> 
     <GSP> 
      <xsl:attribute name="VER"> 
       <xsl:value-of select="3.2" /> 
      </xsl:attribute> 
      <xsl:for-each select="root"> 
       <TM> 
        <xsl:value-of select="(floor(took) div floor(1000))" /> 
       </TM> 

       <RES> 

        <M> 
         <xsl:value-of select="floor(hits/total)" /> 
        </M> 
        <xsl:for-each select="hits/hits"> 
         <xsl:variable name="var1_resultof_first" as="node()" 
          select="_source" /> 

         <R> 
          <xsl:attribute name="N"> 
          <xsl:number format="0" level="single" /> 
          </xsl:attribute> 

          <U> 
           <xsl:sequence 
            select="xs:string(xs:anyURI(fn:string($var1_resultof_first/url)))" /> 
          </U> 
          <UE> 
           <xsl:sequence 
            select="xs:string(xs:anyURI(fn:string($var1_resultof_first/url)))" /> 
          </UE> 
          <UD> 
           <xsl:sequence 
            select="xs:string(xs:anyURI(fn:string($var1_resultof_first/url)))" /> 
          </UD> 

          <T> 
           <xsl:choose> 

            <xsl:when test="highlight/T"> 
             <xsl:for-each select="highlight/T"> 

              <xsl:sequence select="fn:string(.)" /> 

             </xsl:for-each> 
            </xsl:when> 

            <xsl:when test="fn:string($var1_resultof_first/T)"> 
             <xsl:sequence select="fn:string($var1_resultof_first/T)" /> 
            </xsl:when> 

            <xsl:otherwise> 
             <xsl:sequence 
              select="xs:string(xs:anyURI(fn:string($var1_resultof_first/url)))" /> 
            </xsl:otherwise> 
           </xsl:choose> 
          </T> 
          <!-- <T> <xsl:sequence select="fn:string($var1_resultof_first/T)" 
           /> </T> --> 
          <S> 
           <xsl:for-each select="highlight"> 
            &lt;b&gt;... &lt;/b&gt; 
            <xsl:sequence select="fn:string(.)" /> 
            &lt;b&gt; ...&lt;/b&gt; 
           </xsl:for-each> 
          </S> 

          <CRAWLDATE> 
           <xsl:value-of select="substring-before(_source/submitTime,'T')" /> 
          </CRAWLDATE> 
         </R> 
        </xsl:for-each> 
       </RES> 
      </xsl:for-each> 
     </GSP> 
    </xsl:template> 
</xsl:stylesheet> 

你可以看到我输入XML大小元素。

<size>21467</size> 

我想申请这个模板我的尺寸元素(尺寸模板)

<xsl:template match="size"> 
    <HAS> 
     <C SZ="{format-number(. div 1000, '0k')}"/> 
    </HAS> 
</xsl:template> 

我是一个新手,XSLT和XSLT中没有使用多个模板。我应该在我的原始XSLT中将我的尺寸模板放在哪里,以便它可以应用于尺寸参数?任何帮助,将不胜感激。

+1

你在哪里输出你想要它? –

回答

2

的示例尽量减少手头上的问题,请考虑以下样式:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:output method="xml" encoding="UTF-8" indent="yes" /> 

<xsl:template match="/root"> 
    <GSP> 
     <xsl:for-each select="hits/hits"> 
      <R> 
       <UD> 
        <xsl:value-of select="_source/UD" /> 
       </UD> 
       <xsl:apply-templates select="_source/size"/> 
       <UE> 
        <xsl:value-of select="_source/UE" /> 
       </UE> 
      </R> 
     </xsl:for-each> 
    </GSP> 
</xsl:template> 

<xsl:template match="size"> 
    <HAS> 
     <C SZ="{format-number(. div 1000, '0k')}"/> 
    </HAS> 
</xsl:template> 

</xsl:stylesheet> 

应用到你的输入例子,结果将是:

<?xml version="1.0" encoding="UTF-8"?> 
<GSP> 
    <R> 
     <UD>http://www.example.com/big-data-search-managed-services-questions</UD> 
     <HAS> 
     <C SZ="21k"/> 
     </HAS> 
     <UE>http://www.example.com/big-data-search-managed-services-questions</UE> 
    </R> 
</GSP> 
+0

谢谢。将试试这个,让你知道:) – Rose

+0

它的工作。谢谢你的时间 – Rose

2

<xsl:for-each select="hits/hits">循环,你可以使用

<xsl:apply-templates select="_source/size" /> 

无论你想使用什么点大小的模板。

+1

谢谢。有效 :) – Rose