首页 文章

在XSLT中基于属性生成XML元素

提问于
浏览
0

我需要根据属性值生成XML元素 . 我用 <xsl:call-template> . 但以无限循环结束 . 我需要根据以下条件使用基于 <tgroup> 元素的值生成 <colspec> 元素:1 . 根据元素的字符串长度生成 <colspec> 元素2.还生成 <colspec> 元素的属性,如colname = 'col1' colnum = '1',最后align = "left"如果cols属性具有'l'或align = 'right',如果cols属性具有'r',基于值的出现

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

1 回答

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

相关问题