Domanda

I'm trying to have a table with 3 column, based on 2 nodes of an xml file, like this http://jsfiddle.net/4n36W/

XML

<Root>
  <Items>
    <a>10</a>
    <b>20</b>
    <c>30</c>
  </Items>
  <Errors>
    <a>1</a>
    <b>2</b>
    <c>3</c>
  </Errors>
</Root>

My xsl file looks like this, from what I've found in some post of stackoverflow. With this file I have the following error:

XPTY0020: Required item type of the context item for the child axis is node(); supplied
  value has item type xs:integer

XSL

<table>
    <thead>
        <tr>
            <th>Column A</th>
            <th>Column B</th>
            <th>Column C</th>
        </tr>
    </thead>
    <tbody>
        <xsl:for-each select="1 to 3">
            <xsl:variable name="pos" select="position()" />
            <tr>
                <td>
                    <!-- <xsl:value-of select="Root/Items/local-name()" /> -->
                </td>
                <td>
                    <xsl:value-of select="(Root/Items/*)[position()=$pos]" />
                </td>
                <td>
                    <xsl:value-of select="(Root/Errors/*)[position()=$pos]" />
                </td>
            </tr>
        </xsl:for-each>
    </tbody>
</table>
È stato utile?

Soluzione

I would approach this differently, by iterating over the Items/* elements and extracting the matching Errors child by position:

<xsl:for-each select="Root/Items/*">
    <xsl:variable name="pos" select="position()" />
    <tr>
        <td>
            <xsl:value-of select="local-name()" />
        </td>
        <td>
            <xsl:value-of select="." />
        </td>
        <td>
            <xsl:value-of select="../../Errors/*[$pos]" />
        </td>
    </tr>
</xsl:for-each>

Altri suggerimenti

The problem is, you are having a for-each with select="1 to 3". Thus, the context node is an atomic value.

One of the options is to save your root node in variable and use it inside for-each:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <xsl:variable name="Root" select="Root"/>
    <table>
        <thead>
            <tr>
                <th>Column A</th>
                <th>Column B</th>
                <th>Column C</th>
            </tr>
        </thead>
        <tbody>
            <xsl:for-each select="1 to 3">
                <xsl:variable name="pos" select="position()" />
                <tr>
                    <td>
                        <!-- <xsl:value-of select="Root/Items/local-name()" /> -->
                    </td>
                    <td>
                        <xsl:value-of select="$Root/Items/*[position() = $pos]" />
                    </td>
                    <td>
                        <xsl:value-of select="$Root/Errors/*[position()=$pos]" />
                    </td>
                </tr>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top