如何在XSLT中应用多个模板?

时间:2016-09-27 17:15:41

标签: xml templates xslt

这是我的输入XML:

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

这是我申请的XSLT:

<size>21467</size>

您可以在我的输入XML中看到size元素。

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

我想将此模板应用于我的尺寸元素(尺寸模板)

resolve

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:2)

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

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

无论你想使用大小模板。