2013-06-22 42 views
0

我需要根據屬性值生成XML元素。我使用<xsl:call-template>。但以無限循環結束。我需要基於以下條件生成<colspec>元素,其值基於<tgroup>元素: 1.基於元素的字符串長度生成<colspec>元素 2.還生成<colspec>元素的屬性,如colname ='col1'colnum =「1」和最後對齊=「左」,如果cols屬性具有「l」或ALIGN =「右」,如果cols屬性具有基於值的發生爲「r」根據XSLT中的屬性生成XML元素

示例XML:

<table> 
    <tgroup cols="lr"> 
     <thead> 
      <row> 
       <entry>H1</entry> 
       <entry>H1</entry> 
      </row> 
     </thead> 
     <tbody> 
      <row> 
       <entry>B1</entry> 
       <entry>B2</entry> 
      </row> 
     </tbody> 
    </tgroup> 
</table> 

輸出XML:

<table> 
    <tgroup cols="2"> 
     <colspec colnum="1" colname="col1" align="left"/> 
     <colspec colnum="2" colname="col2" align="right"/> 
     <thead> 
      <row> 
       <entry>H1</entry> 
       <entry>H1</entry> 
      </row> 
     </thead> 
     <tbody> 
      <row> 
       <entry>B1</entry> 
       <entry>B2</entry> 
      </row> 
     </tbody> 
    </tgroup> 
</table> 

我嘗試以下XSLT:

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="tgroup"> 
     <xsl:copy> 
      <xsl:attribute name="cols"> 
       <xsl:value-of select="string-length(@cols)"/> 
      </xsl:attribute> 
      <xsl:call-template name="colsp"> 
       <xsl:with-param name="cols_details" select="@cols"/> 
       <xsl:with-param name="cols_count" select="string-length(@cols)"/> 
      </xsl:call-template> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template name="colsp"> 
     <xsl:param name="cols_details"/> 
     <xsl:param name="cols_count"/> 
     <xsl:if test="$cols_count != 0"> 
      <xsl:variable name="single_col" select="substring($cols_details,1,1)"/> 
      <xsl:variable name="cols_details1" select="substring-after($cols_details,$single_col)"/> 
      <xsl:variable name="cols_count1" select="string-length($cols_details)"/> 
      <colspec colnum="{$cols_count1-$cols_count}" colname="col{$cols_count1}" align="{$align}"/> 
      <xsl:call-template name="colsp"> 
       <xsl:with-param name="cols_details1"/> 
       <xsl:with-param name="cols_count1"/> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

回答

0

你可以試試你的XLST的這個固定版本。

<?xml version='1.0'?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="tgroup"> 
     <xsl:copy> 
      <xsl:attribute name="cols"> 
       <xsl:value-of select="string-length(@cols)"/> 
      </xsl:attribute> 
      <xsl:call-template name="colsp"> 
       <xsl:with-param name="cols_details" select="@cols"/> 
      </xsl:call-template> 
      <xsl:apply-templates/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template name="colsp"> 
     <xsl:param name="cols_details"/> 
     <xsl:param name="colnr" select="1"/> 
     <xsl:if test="string-length($cols_details) > 0"> 
      <colspec colnum="{$colnr}" colname="col{$colnr}"> 
       <xsl:variable name="single_col" select="substring($cols_details,1,1)"/> 
       <xsl:attribute name="align"> 
        <xsl:choose> 
         <xsl:when test="$single_col='l'">left</xsl:when> 
         <xsl:when test="$single_col='r'">right</xsl:when> 
         <xsl:when test="$single_col='c'">center</xsl:when> 
        </xsl:choose> 
       </xsl:attribute> 
      </colspec> 
      <xsl:call-template name="colsp"> 
       <xsl:with-param name="cols_details" select="substring($cols_details,2)"/> 
       <xsl:with-param name="colnr" select="$colnr+1"/> 
      </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet> 

這將產生以下輸出:

<?xml version="1.0"?> 
<table> 
    <tgroup cols="3"> 
     <colspec colnum="1" colname="col1" align="left"/> 
     <colspec colnum="2" colname="col2" align="right"/> 
     <colspec colnum="3" colname="col3" align="center"/> 
     <thead> 
      <row> 
       <entry>H1</entry> 
       <entry>H1</entry> 
      </row> 
     </thead> 
     <tbody> 
      <row> 
       <entry>B1</entry> 
       <entry>B2</entry> 
      </row> 
     </tbody> 
    </tgroup> 
</table> 
+0

非常感謝,你的回答工作正常 – siva2012

相關問題