开发者

Using XSLT concat() function in Select of for-each

I am trying to generate HTML which will display the data from XML through XSLT. The XML contains heading tags that are dynamically generated, like h1, h2, h3, h4 etc.

Now in XSLT I want to dynamically access the h1, h2 and h3 in single for-each as the heading can be lesser in level, for example h1, h2 only or can be deeper in 开发者_StackOverflow社区level like h1, h2, h3 and h4.

The output HTML may look like:

                           h1_value1                                h1_value2
           h2_valu1                       h2value3            ....            .....
h3_value1  h3_value2  h3_value3      h3_value4  h3_value5     ....            .....

My XSLT contains a variable that is incremented up to the number of levels which we have already counted. It means that if there are 3 levels then there will h1, h2 and h3 tags in XML. So to access these tags I used concat() function in select of for-each and concatenated "h" with the variable, say j. The template will be recursively called and every time j will be increased by 1 up to the number of levels.

But using a concat() function in select of for-each gives an uncaught error. Can't I use the concat() function in select of for-each or use a variable in select of for-each that is using the concat() function?


You can't

<xsl:for-each select="concat('h', $number)">
</xsl:for-each>

but you can

<xsl:for-each select="*[name() = concat('h', $number)]">
</xsl:for-each>

XPath functions may only be used in node tests (the parts of the expression within square brackets), but not in location steps.

First you must locate nodes in a location step (the * does that here, selecting all child nodes - but you can use any XPath), then you can test a condition on them, for example checking their name.


Maybe xsl:number might come handy here.

If you'd post some sample data, i could provide the xsl i'm thinking of.

What about xsl:sort-ing your hXX nodes?

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

    <xsl:variable name="xdepth" select="matrix/x-depth" />

    <xsl:template match="/">
        <xsl:apply-templates />
    </xsl:template>

    <xsl:template match="h">
        <xsl:apply-templates>
            <xsl:sort select="local-name()" data-type="text" order="ascending" />
        </xsl:apply-templates>
    </xsl:template>

    <xsl:template match="matrix/h/*">
        <tr>
            <xsl:apply-templates />
        </tr>
    </xsl:template>

    <xsl:template match="title">
        <td>
            <xsl:value-of select="." />
        </td>
        <th>
            <xsl:value-of select="." />
        </th>
     </xsl:template>

     <xsl:template match="@* | node()">
         <xsl:copy>
             <xsl:apply-templates select="@* | node()"/>
         </xsl:copy>
     </xsl:template>

     <xsl:template match="x-depth" />
     <xsl:template match="y-depth" />
     <xsl:template match="number-of-measure" />

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜