0

我正在尝试使用 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 进行转换时出现以下异常“状态 Start 中的令牌文本将导致无效的 XML 文档。请确保 ConformanceLevel 设置设置为 ConformanceLevel.Fragment 或 Conformance eLevel.Auto 如果你想写一个 XML 片段。” 如果我从根元素中删除 xmlns 属性,一切正常。为什么会发生这种情况,我该如何解决?

4

1 回答 1

0

将样式表更改为

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>";
于 2011-05-30T11:05:57.697 回答