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.
encoding="UTF-8"
or whatever. But that would only fix one of the problems. – Matthew Green