我有来自 SQLServer 的 XML。我无法让 SQLServer 让我有条件地更改节点的名称,所以我稍后使用 XSLT 进行。我真的不知道我在用 XSLT 做什么——我根据在 StackOverFlow 上搜索到的东西拼凑了我的解决方案。我遇到的问题是输入中没有子级的标签(因此只有一个标签)在转换后扩展为具有开始和结束标签。我想防止这种情况发生,因为我们有足够的用户,带宽是一个问题。
输入是:
<评估数据>
<控件>
<questRequiredOverride>N</questRequiredOverride>
</控件>
<路径>
<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>
<序列>
<组>
<content_blockpresentation="情境判断指令"
类型="说明">
<问题>
<question id="sjex"/>
</问题>
</content_block>
<content_block 演示文稿="SituationalJudgmentQuestions" type="exercise"
路径="1">
<问题>
<question id="sj6_Mgt"/>
<question id="sj7_Mgt"/>
</问题>
</content_block>
<content_block 演示文稿="SituationalJudgmentQuestions" type="exercise"
路径=“2”>
<问题>
<question id="sj13_SS"/>
<question id="sj12_SS"/>
<question id="sj10_SS"/>
<question id="sj8_SS"/>
<question id="sj5_SS"/>
<question id="sj3_SS"/>
</问题>
</content_block>
<content_blockpresentation="Intermission" type="intermission"/>
</组>
</序列>
</路径>
...
<path id="计分">
<序列>
<组>
<content_block presentation="计分" type="计分"/>
</组>
</序列>
</路径>
<path id="反馈">
<questionFile timeScreen="" timeEstimate="0">反馈-CVS</questionFile>
<序列>
<组>
<content_block 演示文稿="Feedback" type="exercise" path="1">
<问题>
<question id="fb30"/>
<question id="fb32"/>
<question id="fb40"/>
<question id="fb50"/>
</问题>
</content_block>
</组>
</序列>
</路径>
</路径>
</评估数据>
XSLT 是
<?xml 版本="1.0" 编码="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
版本="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:元素>
</xsl:模板>
<xsl:template match="@*|node()">
<xsl:复制>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:模板>
</xsl:样式表>
...输出是:
<评估数据>
<控件>
<questRequiredOverride>N</questRequiredOverride>
</控件>
<路径>
<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>
<序列>
<组>
<instructions presentation="SituationalJudgmentInstructions" type="instructions">
<问题>
<question id="sjex"> </question>
</问题>
</说明>
<exercise presentation="SituationalJudgmentQuestions" type="exercise" path="1">
<问题>
<question id="sj6_Mgt"> </question>
<question id="sj7_Mgt"> </question>
</问题>
</练习>
<exercise presentation="SituationalJudgmentQuestions" type="exercise" path="2">
<问题>
<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>
</问题>
</练习>
<intermissionpresentation="Intermission" type="intermission"> </intermission>
</组>
</序列>
</路径>
...
<path id="计分">
<序列>
<组>
<scoring presentation="Scoring" type="scoring"> </scoring>
</组>
</序列>
</路径>
<path id="反馈">
<questionFile timeScreen="" timeEstimate="0">反馈-CVS</questionFile>
<序列>
<组>
<练习演示=“反馈”类型=“练习”路径=“1”>
<问题>
<question id="fb30"> </question>
<question id="fb32"> </question>
<question id="fb40"> </question>
<question id="fb50"> </question>
</问题>
</练习>
</组>
</序列>
</路径>
</路径>
</评估数据>
请注意每个问题标签现在是如何“爆炸”的。在真正的 XML 中,比我在这里展示的要多得多。
将这个问题放在一起时我注意到的一件事是,转换还向转换后的 XML 添加了 UTF-16 编码。如果有人对如何解决这个问题有任何想法,也欢迎:)。
更新
我正在将 XML 和 XSL 加载到 ASP Classic 中的 MSXML2.DOMDocument.3.0 中并使用 transformNode。我已经设法通过在结果字符串上使用 Replace 来修复 UTF 编码,但我对解决方案并不满意。