简单的嵌套XSLT解决方案

时间:2015-11-03 19:13:19

标签: xml xslt

这是我的XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <clubs>
        <club>
            <buttons>
                <button>
                    <name>rating</name>
                    <generalclassname>rating</generalclassname>
                    <numericclassname>score</numericclassname>
                    <valuedata>1</valuedata>
                    <locid>1</locid>
                    <type>numcomments</type>
                    <label>Comments</label>
                    <icon>data:image/gif;base64,R0lGODlhDQAMAMQAAP////z8/Pn5+fX19e3t7evr6+np6eTk5ODg4N7e3tnZ2dfX18zMzMrKysPDw729vaurq6ampqKioqCgoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAADQAMAAAFQyAgjs1omsmUnGYATVDAAgIz3YwwLs97/7DHghABGiMEQKFonEQKI0NzYjAJproRYZocISYSx8EhmSBMCsZgNGAoRCEAOw==</icon>
                </button>
                <button>
                    <name>reviews</name>
                    <generalclassname>reviews</generalclassname>
                    <numericclassname>numreviews</numericclassname>
                    <valuedata>9</valuedata>
                    <locid>9</locid>
                    <type>numcomments</type>
                    <label>Comments</label>
                   <icon>data:image/gif;base64,R0lGODlhCwALAMQAAP////7+/v39/fr6+vn5+ff39/Dw8O7u7ufn5+Xl5eDg4Nvb29fX19bW1tDQ0MrKysbGxsTExMHBwcDAwLu7u7m5ubi4uLW1tbOzs7GxsbCwsK2traysrKurqwAAAAAAACwAAAAACwALAAAFOSAgjoEUjSjAdFcqKltXpQXU3YroPEh23xpKghNZ/Y6dIvKGSRqRDueyc5gokRaAgDAwLCC+TgMQAgA7</icon>
                </button>
                <button>
                    <name>comments</name>
                    <generalclassname>comments</generalclassname>
                    <numericclassname>numcomments</numericclassname>
                    <valuedata>8626</valuedata>
                    <locid>8626</locid>
                    <type>numcomments</type>
                    <label>Comments</label>
                    <icon>data:image/gif;base64,R0lGODlhCwALAKIAAP////7+/vX19c/Pz8LCwrCwsKCgoAAAACwAAAAACwALAAADHShT1l1DudkWnezqnTXcRmRhEWA2RGCuZ8qy7psAADs=</icon>
                </button>
            </buttons>
        </club>
    </clubs>

这是我的XSLT:

    <xsl:for-each select="./clubs/club">
    <xsl:for-each select="./buttons/button">
    <div style="bottom:50px">
    <xsl:attribute name="class" ><xsl:value-of select="/.generalclassname"/></xsl:attribute>
    <div>
    <xsl:attribute name="class"><xsl:value-of select="/.numericclassname"/></xsl:attribute>
    <xsl:value-of select="/.valuedata"/>
    </div><span><xsl:valueof select="./label"/></span>
    </div>
    </xsl:for-each>
    </xsl:for-each>

我不认为我正确地查询和编码。

我生成的HTML应该是:

<div class="comments"><div class="numcomments">1</div><span>Comments</span></div>

这是对阵阵列中的第三条记录。

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:1)

select="/.generalclassname"等表达式更改为select="generalclassname"。不过,我建议使用属性值模板简化代码:<div class="{generalclassname}">

答案 1 :(得分:0)

试试这个:

<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:template match="button">
<div style="bottom:50px">
    <xsl:attribute name="class"><xsl:value-of select="generalclassname"/></xsl:attribute>
    <div>
    <xsl:attribute name="class"><xsl:value-of select="numericclassname"/></xsl:attribute>
            <xsl:value-of select="valuedata"/>
    </div>
    <span>
        <xsl:value-of select="label"/> 
    </span>
</div>
</xsl:template>