我有来自SQLServer的XML . 我无法让SQLServer让我有条件地更改节点的名称,所以我稍后使用XSLT进行操作 . 我真的不知道我在使用XSLT做什么 - 我已经从我在StackOverFlow上搜索的内容中拼凑了我的解决方案 . 我遇到的问题是,在输入中没有子节点的标签(因此只有一个标签)被扩展为在转换后具有开始和结束标记 . 我想阻止这一点,因为我们有足够的用户,带宽是一个问题 .
输入是:
<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>
XSLT是
<?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>
......输出是:
<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>
请注意每个问号标签现在是如何“爆炸”的 . 在真正的XML中,有很多东西比我在这里展示的要多得多 .
将这个问题放在一起时我注意到的一件事是,转换也是为转换后的XML添加UTF-16编码 . 如果有人对如何解决这个问题有任何想法,那也欢迎:) .
Update
我已经设法通过在结果字符串上使用Replace来修复UTF编码,但我对解决方案并不满意 .
1 回答
出于某种原因,从Msxml2.DomDocument.3.0更改为Msxml2.DomDocument.6.0修复了第一个问题 . 甚至在XSLT中设置编码也没有解决第二个问题(这似乎是transformNode的一个已知问题) . 相反,我使用了omit-xml-declaration =“yes”,它实际上没有省略XML声明但是没有留下编码值 . 对于我正在使用它的东西,这对我来说已经足够了 .