微软BI 之SSIS 系列 - XML Task 中XSLT 样式表转换错误记录

本文探讨了XSLT样式表转换过程中常见的语法错误——属性与值的顺序不当,并提供了正确的编写方式及输出示例。

开篇介绍

此文章专门记录 XSLT 样式表转换过程中的语法问题

错误一 值与属性的倒置

修改了几次样式表,但还是一如既往的报错,报错信息如下:

[XML Task] Error: An error occurred with the following error message: "Attribute and namespace nodes cannot be added to the parent element after a text, comment, pi, or sub-element node has already been added.".

很明显 XML Task 已经告知了错误的原因,即属性或者命名空间的定义不能跟随已经存在的文之后。

正确的写法:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/"> 
    <newsalesorder>
    <xsl:for-each select="SalesOrder/SalesOrderDetail">
        <salesorderdetail>
            <salesorderID>
                <xsl:attribute name="detailID">
                    <xsl:value-of select="SalesOrderDetailID"/>
                </xsl:attribute>
                <xsl:value-of select="SalesOrderID"/>
            </salesorderID> 
        </salesorderdetail>
    </xsl:for-each> 
    </newsalesorder>
</xsl:template>
</xsl:stylesheet> 

错误的写法 - SalesOrderID 在 XML Task 转换时,源 SalesOrderID 的值一经写出,后面再跟 Attribute 是不正确的,应该是 Attribute 在前。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/"> 
    <newsalesorder>
    <xsl:for-each select="SalesOrder/SalesOrderDetail">
        <salesorderdetail>
            <salesorderID>
                <xsl:value-of select="SalesOrderID"/>
                <xsl:attribute name="detailID">
                    <xsl:value-of select="SalesOrderDetailID"/>
                </xsl:attribute>
            </salesorderID> 
        </salesorderdetail>
    </xsl:for-each> 
    </newsalesorder>
</xsl:template>
</xsl:stylesheet> 

正确的输出片段中 detailID 这个 Attribute 属性应该要定义在 SalesOrderID 71774 之前。

<?xml version="1.0" encoding="utf-8"?>
<newsalesorder>
  <salesorderdetail>
    <salesorderID detailID="110562">71774</salesorderID>
  </salesorderdetail>
  <salesorderdetail>
    <salesorderID detailID="110563">71774</salesorderID>
  </salesorderdetail>
  <salesorderdetail>
    <salesorderID detailID="110567">71776</salesorderID>
  </salesorderdetail>

相关资源

XSLT 教程

更多 BI 文章请参看 BI 系列随笔列表 (SSIS, SSRS, SSAS, MDX, SQL Server)  如果觉得这篇文章看了对您有帮助,请帮助推荐,以方便他人在 BIWORK 博客推荐栏中快速看到这些文章。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值