kelvingo935
kelvingo935

Reputation: 23

How XSLT generate a dynamic variable array from XML?

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)))))'.

Upvotes: 0

Views: 10348

Answers (2)

Siva Charan
Siva Charan

Reputation: 18064

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>

Upvotes: 2

Sean B. Durkin
Sean B. Durkin

Reputation: 12729

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>

Upvotes: 0

Related Questions