1

I have some XML that looks something like this:

  <ExtensionObject>
    <Value xmlns="">
        <Key>key01</Key>
        <StringValue>somewords</StringValue>
    </Value>
    <Value xmlns="">
        <Key>key01</Key>
        <NumberValue>12345</NumberValue>
    </Value>

...........hundreds more Values..............
  </ExtensionObject>

I rather optimistically wrote XSLT like this:

<xsl:template match="/ExtensionObject">
    <VResult>
        <xsl:for-each select="Value">
            <xsl:variable name="fld">
                <Value><xsl:value-of select="Key"/></Value>
            </xsl:variable>
            <Result field="$fld">
                <Value>
                    <xsl:choose>
                        <xsl:when test="NumberValue">
                            <xsl:value-of select="NumberValue"/>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="StringValue"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </Value>
            </Result>
        </xsl:for-each>
    </VResult>
</xsl:template>

And expected to get this:

<VResult>
    <Result field="key01"><Value>somewords</Value></Result>
    <Result field="key02"><Value>12345</Value></Result>
    ...........hundreds more Values..............
</VResult>

Instead I got:

<VResult>
    <Result field="$fld"><Value>somewords</Value></Result>
    <Result field="$fld"><Value>12345</Value></Result>
    ...........hundreds more Values..............
</VResult>

Does anyone know how I can insert the field names with the key value?

2 Answers 2

3
<xsl:attribute name="field"><xsl:value-of select="Key"/></xsl:attribute>
Sign up to request clarification or add additional context in comments.

1 Comment

Yep. Or, <Result field="{Key}">.
1

Instead of using for-each, it's always better to use the templates:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <VResults>
            <xsl:apply-templates select='*' />
        </VResults>
    </xsl:template>
    <xsl:template match="Value">
        <Result>
            <xsl:attribute name='field'><xsl:value-of select='Key' /></xsl:attribute>
            <xsl:apply-templates />
        </Result>
    </xsl:template>
    <xsl:template match="NumberValue|StringValue">
        <Value><xsl:value-of select='.' /></Value>
    </xsl:template>
    <xsl:template match="text()" />
</xsl:stylesheet>

2 Comments

Always? If that were the case, I don't think the language designers would have gone to the expense of including for-each in the definition. I write a lot of XSLT, and sometimes for-each makes a lot more sense. It's less powerful, which means it's more predictable, therefore easier to trace and debug.
Agreed. I should have made my comment less final. I should have said something like: it's preferrable when the template matching is obvious.

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.