1
votes

I have XML coming out of SQLServer. I couldn't get SQLServer to let me conditionally change the name of a node, so I do it later with XSLT. I don't really know what I'm doing with XSLT--I've cobbled my solution together from things I found searching on StackOverFlow. The problem I'm having is that tags that have no children in the input (and hence have only one tag) are expanded out to have an opening and closing tag after the transformation. I'd like to prevent this, since we have enough users that bandwidth is a concern.

The input is:

<assessdata>
    <controls>
        <questRequiredOverride>N</questRequiredOverride>
    </controls>
    <paths>
        <path id="SJ">
            <questionFile timeScreen="" timeEstimate="0">SJ-CVS-Section-Mgt</questionFile>
            <questionFile timeScreen="SitJudge" timeEstimate="5">SJ-CVS-Mgt</questionFile>
            <questionFile timeScreen="SitJudge" timeEstimate="5">SJ-CVS-Mgt-SS</questionFile>
            <sequence>
                <group>
                    <content_block presentation="SituationalJudgmentInstructions"
                        type="instructions">
                        <questions>
                            <question id="sjex"/>
                        </questions>
                    </content_block>
                    <content_block presentation="SituationalJudgmentQuestions" type="exercise"
                        path="1">
                        <questions>
                            <question id="sj6_Mgt"/>
                            <question id="sj7_Mgt"/>
                        </questions>
                    </content_block>
                    <content_block presentation="SituationalJudgmentQuestions" type="exercise"
                        path="2">
                        <questions>
                            <question id="sj13_SS"/>
                            <question id="sj12_SS"/>
                            <question id="sj10_SS"/>
                            <question id="sj8_SS"/>
                            <question id="sj5_SS"/>
                            <question id="sj3_SS"/>
                        </questions>
                    </content_block>
                    <content_block presentation="Intermission" type="intermission"/>
                </group>
            </sequence>
        </path>
...
        <path id="Scoring">
            <sequence>
                <group>
                    <content_block presentation="Scoring" type="scoring"/>
                </group>
            </sequence>
        </path>
        <path id="Feedback">
            <questionFile timeScreen="" timeEstimate="0">Feedback-CVS</questionFile>
            <sequence>
                <group>
                    <content_block presentation="Feedback" type="exercise" path="1">
                        <questions>
                            <question id="fb30"/>
                            <question id="fb32"/>
                            <question id="fb40"/>
                            <question id="fb50"/>
                        </questions>
                    </content_block>
                </group>
            </sequence>
        </path>
    </paths>
</assessdata>

The XSLT is

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="content_block">
        <xsl:element name="{@type}">
        <xsl:apply-templates select="@*|node()"></xsl:apply-templates>
        </xsl:element>
    </xsl:template>
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

...and the output is:

<assessdata>
    <controls>
        <questRequiredOverride>N</questRequiredOverride>
    </controls>
    <paths>
        <path id="SJ">
            <questionFile timeScreen="" timeEstimate="0">SJ-CVS-Section-Mgt</questionFile>
            <questionFile timeScreen="SitJudge" timeEstimate="5">SJ-CVS-Mgt</questionFile>
            <questionFile timeScreen="SitJudge" timeEstimate="5">SJ-CVS-Mgt-SS</questionFile>
            <sequence>
                <group>
                    <instructions presentation="SituationalJudgmentInstructions" type="instructions">
                        <questions>
                            <question id="sjex"> </question>
                        </questions>
                    </instructions>
                    <exercise presentation="SituationalJudgmentQuestions" type="exercise" path="1">
                        <questions>
                            <question id="sj6_Mgt"> </question>
                            <question id="sj7_Mgt"> </question>
                        </questions>
                    </exercise>
                    <exercise presentation="SituationalJudgmentQuestions" type="exercise" path="2">
                        <questions>
                            <question id="sj13_SS"> </question>
                            <question id="sj12_SS"> </question>
                            <question id="sj10_SS"> </question>
                            <question id="sj8_SS"> </question>
                            <question id="sj5_SS"> </question>
                            <question id="sj3_SS"> </question>
                        </questions>
                    </exercise>
                    <intermission presentation="Intermission" type="intermission"> </intermission>
                </group>
            </sequence>
        </path>
...
        <path id="Scoring">
            <sequence>
                <group>
                    <scoring presentation="Scoring" type="scoring"> </scoring>
                </group>
            </sequence>
        </path>
        <path id="Feedback">
            <questionFile timeScreen="" timeEstimate="0">Feedback-CVS</questionFile>
            <sequence>
                <group>
                    <exercise presentation="Feedback" type="exercise" path="1">
                        <questions>
                            <question id="fb30"> </question>
                            <question id="fb32"> </question>
                            <question id="fb40"> </question>
                            <question id="fb50"> </question>
                        </questions>
                    </exercise>
                </group>
            </sequence>
        </path>
    </paths>
</assessdata>

Note how each question tag is now "exploded." In the real XML, there are a lot more than I've shown here.

One thing I noticed while putting this question together is that the transformation is also adding a UTF-16 encoding to the transformed XML as well. If anyone has any thoughts on how to fix that, it would also be welcome :).

Update
I'm loading both the XML and XSL into MSXML2.DOMDocument.3.0 in ASP Classic and using transformNode. I've managed to fix the UTF encoding by using Replace on the resulting string, but I'm not that happy with the solution.

1
i guess both issues come not from the XSLT you wrote, but from the XSLT processor itself. Please specify which processor you're using and all parameters involved.Javier
Edited to address commentAmy Blankenship
I can back up Javier's response. This is most likely caused by your processor. I ran the provided transform using Xalan and I'm not seeing either issue of the XML being 'exploded' or the UTF-16 issue.Matthew Green
Also, you can set your encoding in the output tag by doing encoding="UTF-8" or whatever. But that would only fix one of the problems.Matthew Green

1 Answers

1
votes

For some reason, changing from Msxml2.DomDocument.3.0 to Msxml2.DomDocument.6.0 fixed the first problem. Even setting the encoding in the XSLT didn't fix the second problem (which seems to be a known problem with transformNode). Instead, I used omit-xml-declaration="yes", which doesn't actually omit the XML declaration but does leave off the encoding value. For what I'm using it for, that's good enough for me.