0

I am using XSLT to transform XML to HTML. Currently i am trying to figured out how can i make an array variable in XSLT.

This is my XML :

<data>
    <ACCOUNTS elem="0">
        <ACCOUNT_NO>12345</ACCOUNT_NO>
    </ACCOUNTS>
    <ACCOUNTS elem="1">
        <ACCOUNT_NO>67890</ACCOUNT_NO>
    </ACCOUNTS>
</data>

This is my the XSLT that i try to figured out but is not working :

<xsl:variable name="accounts">
    <xsl:for-each select="data/ACCOUNTS">
        <child elem="<xsl:value-of select='position()' />"><xsl:value-of select="ACCOUNT_NO" /></child>
    </xsl:for-each>
</xsl:variable>

How can i add the position into the XML node so that i'm able to display it out by using

<xsl:value-of select="$account[@elem=1]" />

Please help me and let me know if got any concern. Thanks.

UPDATED :

My full XSLT code :

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

<xsl:variable name="accounts">
    <xsl:for-each select="data/ACCOUNTS">
        <child elem="{@elem}"><xsl:value-of select="ACCOUNT_NO" /></child>
    </xsl:for-each>
</xsl:variable>

<xsl:template match="/">
    <html>
        <head><title>testing</title></head>
        <body>
            <xsl:value-of select="$accounts/child[@elem='0']" />
        </body>
    </html>
</xsl:template>
</xsl:stylesheet>

And when i compile i get this error :

JAXPSAXProcessorInvoker  - Error checking type of the expression 'FilterParentPath(variable-ref(accounts/result-tree), step("child", 40, pred(=(step("attribute", 18), literal-expr(0)))))'.
1
  • It's a little unclear what you are trying to achieve. Could you add the desired html output? Maybe there's a much simpler solution than your current approach. Commented May 30, 2012 at 7:43

2 Answers 2

2

If my understanding is correct,

INPUT XML:

<data>
    <ACCOUNTS elem="0">
        <ACCOUNT_NO>12345</ACCOUNT_NO>
    </ACCOUNTS>
    <ACCOUNTS elem="1">
        <ACCOUNT_NO>67890</ACCOUNT_NO>
    </ACCOUNTS>
</data>

XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:for-each select="data/ACCOUNTS">
            <child elem="{@elem}">
                <xsl:value-of select="ACCOUNT_NO"/>
            </child>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

OUTPUT HTML:

<child elem="0">12345</child>
<child elem="1">67890</child>

ANOTHER XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <xsl:variable name="accounts">
            <xsl:for-each select="data/ACCOUNTS">
                <child elem="{@elem}">
                    <xsl:value-of select="ACCOUNT_NO"/>
                </child>
            </xsl:for-each>
        </xsl:variable>
        <xsl:value-of select="$accounts/child[@elem='1']"/>
    </xsl:template>
</xsl:stylesheet>

OUTPUT:

67890

UPDATED:

@Kelvingo: As per your updated XSLT, I feel you may require msxsl:node-set to use this

<xsl:value-of select="msxsl:node-set($accounts)/child[@elem='0']"/>

accounts variable.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:output method="html" encoding="utf-8" indent="yes"/>
    <xsl:variable name="accounts">
        <xsl:for-each select="data/ACCOUNTS">
            <child elem="{@elem}">
                <xsl:value-of select="ACCOUNT_NO"/>
            </child>
        </xsl:for-each>
    </xsl:variable>
    <xsl:template match="/">
        <html>
            <head>
                <title>testing</title>
            </head>
            <body>
                <xsl:value-of select="msxsl:node-set($accounts)/child[@elem='0']"/>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
Sign up to request clarification or add additional context in comments.

3 Comments

i get this error while transform using Eclipse.<br/> JAXPSAXProcessorInvoker - Error checking type of the expression 'FilterParentPath(variable-ref(accounts/result-tree), step("child", 40, pred(=(step("attribute", 18), literal-expr(0)))))'.
Siva, while your answer is beautifully formatted and would be correct for a compliant XSLT engine, you have missed the fine print of his question. From the error message he is using an Eclipse XSL plugin. Your solutions won't work in his broken down XSLT engine.
@kelvingo935: You may required to use msxsl:node-set for accessing the variable. Try this, I have updated the answer.
0

With your particular XSLT engine has a requirement that if the output method is HTML then there must be exactly one output root element.

My solution (not tested) is a tweak of Siva's. I've just ensured a one-root element output.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
       <html> 
        <xsl:for-each select="data/ACCOUNTS">
            <child elem="{@elem}">
                <xsl:value-of select="ACCOUNT_NO"/>
            </child>
        </xsl:for-each>
      </html>
    </xsl:template>
</xsl:stylesheet>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.