问题描述
|
我正在尝试使用C#中的xslcompiledtransform将xml从一种格式转换为另一种格式。下面是样式表和源xml,
string xslMarkup = @\"<?xml version=\'1.0\'?>
<xsl:stylesheet xmlns:xsl=\'http://www.w3.org/1999/XSL/Transform\' xmlns:msxsl=\'urn:schemas-microsoft-com:xslt\' exclude-result-prefixes=\'msxsl\' version=\'1.0\'>
<xsl:output method=\'xml\' indent=\'yes\'/>
<xsl:template match=\'/NewDataSet\'>
<Root>
<xsl:apply-templates select=\'Violations\'/>
</Root>
</xsl:template>
<xsl:template match=\'Violations\'>
<detail ccr_id=\'{@ViolationID}\'
assn_id=\'\'
member_id=\'\'
local_ccr_id=\'\'
create_date=\'\'
inspect_date=\'\'
respond_date=\'\'
last_inspect_date=\'\'
status=\'\'
active=\'\'
type=\'\'
description=\'\'
type_desc=\'\'
owner_action=\'\'
acc_action=\'\'
acc_action_date=\'\'
last_acc_action=\'\'
last_acc_action_date=\'\'
ccr_name=\'\'
location=\'\' />
</xsl:template>
</xsl:stylesheet>\";
XDocument xmlTree =
XDocument.Parse(@\"<?xml version=\'1.0\' encoding=\'utf-8\'?>
<NewDataSet xmlns=\'www.reefpt.com/caliberapi\'>
<Violations>
<ViolationID>66</ViolationID>
<ViolationNumber>201fgh4</ViolationNumber>
</Violations>
<Violations>
<ViolationID>66</ViolationID>
<ViolationNumber>2011fgh</ViolationNumber>
</Violations>
</NewDataSet>
\");
使用状态为“开始”的xslcompiledtransform \“ Token Text”进行转换时,出现以下异常,将导致无效的XML文档。请确保将ConformanceLevel设置设置为ConformanceLevel.Fragment或Conformanc eLevel。如果要编写XML片段,则为“自动”。
如果我从根元素中删除xmlns属性,则一切正常。为什么会发生这种情况,我该如何解决?
解决方法
将样式表更改为
string xslMarkup = @\"<?xml version=\'1.0\'?>
<xsl:stylesheet xmlns:xsl=\'http://www.w3.org/1999/XSL/Transform\' xmlns:msxsl=\'urn:schemas-microsoft-com:xslt\' xmlns:df=\"www.reefpt.com/caliberapi\" exclude-result-prefixes=\'msxsl df\' version=\'1.0\'>
<xsl:output method=\'xml\' indent=\'yes\'/>
<xsl:template match=\'/df:NewDataSet\'>
<Root>
<xsl:apply-templates select=\'df:Violations\'/>
</Root>
</xsl:template>
<xsl:template match=\'df:Violations\'>
<detail ccr_id=\'{@ViolationID}\'
assn_id=\'\'
member_id=\'\'
local_ccr_id=\'\'
create_date=\'\'
inspect_date=\'\'
respond_date=\'\'
last_inspect_date=\'\'
status=\'\'
active=\'\'
type=\'\'
description=\'\'
type_desc=\'\'
owner_action=\'\'
acc_action=\'\'
acc_action_date=\'\'
last_acc_action=\'\'
last_acc_action_date=\'\'
ccr_name=\'\'
location=\'\' />
</xsl:template>
</xsl:stylesheet>\";