2015-09-22 3 views
0

Мне нужно добавить оболочку вокруг вывода многих шаблонов. В приведенном ниже примере показано, что я пытаюсь сделать.Wrap Template Output in Elements

Мой текущий шаблон делает прямые вызовы для выходных данных, outputTable шаблонов.

Теперь мне нужно обернуть вывод каждого шаблона в пару div. Это происходит много (много!) Раз в моем XSL, поэтому я ищу решение, которое не требует добавления шаблона оболочки до и после каждого вызова.

Я попытался добавить шаблон xsl: call в шаблоне xsl: call-template. Я также попытался передать имя шаблона в качестве параметра (пример ниже).

Можно ли добавить код до и после каждого вызова шаблона без дублирования кода оболочки повсюду?

Пример XSL

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/> 

<xsl:template match='/' > 
<html> 
    <body> 
    <div class='content'> 
     <div class='row'> 
      <div class='col-sm-6'> 
       <!-- 
        Original Call straight to the template 
        <xsl:call-template name='outputText'> 
       --> 
       <xsl:call-template name='wrapper'> 
        <xsl:with-param name='outputText'/> 
       </xsl:call-template> 
      </div> 

      <div class='col-sm-6'> 
       <!-- 
        Original Call straight to the template 
        <xsl:call-template name='outputTable'> 
       --> 
       <xsl:call-template name='wrapper'> 
        <xsl:with-param name='outputTable'/> 
       </xsl:call-template> 
      </div> 
     </div> 
    </div> 
    </body> 
</html> 
</xsl:template> 

<xsl:template name='wrapper'> 
    <xsl:param name='template'/> 
    <div class='top-wrapper'> 
     <div class='wrappercontent'> 
      <h1>Content Wrapper</h1> 
     </div> 
     <div class='inner-wrapper'> 
      <xsl:call-template name='{$template}'/> 
     </div> 
    </div> 
</xsl:template> 

<xsl:template name='outputText'> 
    <h1>Test Header</h1> 
    <p>Test content</p> 
</xsl:template> 

<xsl:template name='outputTable'> 
    <h1>Test Table</h1> 
    <table> 
     <tr> 
      <th>Name</th> 
      <th>Age</th> 
     </tr> 
     <xsl:for-each select='/xml/character'> 
      <tr> 
       <td><xsl:value-of select='@name'/></td> 
       <td><xsl:value-of select='@age'/></td> 
      </tr> 
     </xsl:for-each> 
    </table> 
</xsl:template> 
</xsl:stylesheet> 

Пример XML

<xml> 
    <character name='Zaphod Beeblerox' age='100'/> 
    <character name='Harry Potter' age='13'/> 
</xml> 

Нужный результат будет выглядеть следующим образом

<html> 
    <body> 
     <div class="content"> 
      <div class="row"> 
       <div class="col-sm-6"> 
        <div class="top-wrapper"> 
         <div class="wrappercontent"> 
          <h1>Content Wrapper</h1> 
         </div> 
         <div class="inner-wrapper"> 
          <h1>Test Header</h1> 
          <p>Test content</p> 
         </div> 
        </div> 
       </div> 
       <div class="col-sm-6"> 
        <div class="top-wrapper"> 
         <div class="wrappercontent"> 
          <h1>Content Wrapper</h1> 
         </div> 
         <div class="inner-wrapper"> 
          <h1>Test Table</h1> 
          <table> 
           <tr> 
            <th>Name</th> 
            <th>Age</th> 
           </tr> 
           <tr> 
            <td>Zaphod Beeblerox</td> 
            <td>100</td> 
           </tr> 
           <tr> 
            <td>Harry Potter</td> 
            <td>13</td> 
           </tr> 
          </table> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
    </body> 
</html> 
+0

Пожалуйста, покажите свой ожидаемый выход для ввода образца. –

ответ

1

Вы можете использовать микро-конвейерную, такие как ...

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
<xsl:output method="html" indent="yes" encoding="utf-8" version="5" doctype-system="" /> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/"> 
    <hmtl> 
    <head> 
     <title>Hitchhiker's guide</title> 
    </head> 
    <body> 
     <xsl:apply-templates select="xml" /> 
    </body> 
    </hmtl> 
</xsl:template> 

<xsl:template match="xml"> 
    <div class='content'> 
    <div class='row'> 
     <div class='col-sm-6'> 
     <xsl:variable name="content-to-be-wrapped"> 
      <xsl:call-template name="outputText" /> 
     </xsl:variable> 
     <xsl:call-template name="Wrap"> 
      <xsl:with-param name="content" select="$content-to-be-wrapped" /> 
     </xsl:call-template> 
     </div> 

     <div class='col-sm-6'> 
     <xsl:variable name="content-to-be-wrapped"> 
      <xsl:call-template name="outputTable" /> 
     </xsl:variable> 
     <xsl:call-template name="Wrap"> 
      <xsl:with-param name="content" select="$content-to-be-wrapped" /> 
     </xsl:call-template> 
     </div> 
    </div> 
    </div> 
</xsl:template> 

<xsl:template name="Wrap"> 
    <xsl:param name="content" /> 
    <div class='top-wrapper'> 
    <div class='wrappercontent'><h1>Content Wrapper</h1></div> 
    <div class='inner-wrapper'> 
     <xsl:copy-of select="$content" /> 
    </div> 
    </div> 
</xsl:template> 

<xsl:template name="outputText"> 
    <h1>Test Header</h1> 
    <p>Test content</p> 
</xsl:template> 

<xsl:template name="outputTable"> 
    <h1>Test Table</h1> 
    <table> 
    <tr><th>Name</th><th>Age</th></tr> 
    <xsl:apply-templates select="character" /> 
    </table> 
</xsl:template> 

<xsl:template match="character"> 
    <tr> 
    <td><xsl:value-of select='@name'/></td> 
    <td><xsl:value-of select='@age'/></td> 
    </tr> 
</xsl:template> 

</xsl:stylesheet> 

... или непосредственно, таким образом, ...

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
<xsl:output method="html" indent="yes" encoding="utf-8" version="5" doctype-system="" /> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/"> 
    <hmtl> 
    <head> 
     <title>Hitchhiker's guide</title> 
    </head> 
    <body> 
     <xsl:apply-templates select="xml" /> 
    </body> 
    </hmtl> 
</xsl:template> 

<xsl:template match="xml"> 
    <div class='content'> 
    <div class='row'> 
     <div class='col-sm-6'> 
     <xsl:call-template name="Wrap"> 
      <xsl:with-param name="content"> 
      <xsl:call-template name="outputText" /> 
      </xsl:with-param> 
     </xsl:call-template> 
     </div> 

     <div class='col-sm-6'> 
     <xsl:call-template name="Wrap"> 
      <xsl:with-param name="content"> 
      <xsl:call-template name="outputTable" /> 
      </xsl:with-param> 
     </xsl:call-template> 
     </div> 
    </div> 
    </div> 
</xsl:template> 

<xsl:template name="Wrap"> 
    <xsl:param name="content" /> 
    <div class='top-wrapper'> 
    <div class='wrappercontent'><h1>Content Wrapper</h1></div> 
    <div class='inner-wrapper'> 
     <xsl:copy-of select="$content" /> 
    </div> 
    </div> 
</xsl:template> 

<xsl:template name="outputText"> 
    <h1>Test Header</h1> 
    <p>Test content</p> 
</xsl:template> 

<xsl:template name="outputTable"> 
    <h1>Test Table</h1> 
    <table> 
    <tr><th>Name</th><th>Age</th></tr> 
    <xsl:apply-templates select="character" /> 
    </table> 
</xsl:template> 

<xsl:template match="character"> 
    <tr> 
    <td><xsl:value-of select='@name'/></td> 
    <td><xsl:value-of select='@age'/></td> 
    </tr> 
</xsl:template> 

</xsl:stylesheet> 

приведенной выше таблицы стилей XSLT 1.0, при нанесении на входе ...

<xml> 
    <character name='Zaphod Beeblerox' age='100'/> 
    <character name='Harry Potter' age='13'/> 
</xml> 

... выход выход HTML страницы ...

<!DOCTYPE html SYSTEM ""> 
<hmtl> 
    <head> 
    <META http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title>Hitchhiker's guide</title> 
    </head> 
    <body> 
    <div class="content"> 
     <div class="row"> 
     <div class="col-sm-6"> 
      <div class="top-wrapper"> 
      <div class="wrappercontent"> 
       <h1>Content Wrapper</h1> 
      </div> 
      <div class="inner-wrapper"> 
       <h1>Test Header</h1> 
       <p>Test content</p> 
      </div> 
      </div> 
     </div> 
     <div class="col-sm-6"> 
      <div class="top-wrapper"> 
      <div class="wrappercontent"> 
       <h1>Content Wrapper</h1> 
      </div> 
      <div class="inner-wrapper"> 
       <h1>Test Table</h1> 
       <table> 
       <tr> 
        <th>Name</th> 
        <th>Age</th> 
       </tr> 
       <tr> 
        <td>Zaphod Beeblerox</td> 
        <td>100</td> 
       </tr> 
       <tr> 
        <td>Harry Potter</td> 
        <td>13</td> 
       </tr> 
       </table> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
    </body> 
</hmtl> 
+0

Точно, что я искал, СПАСИБО Шон –