【发布时间】:2017-07-11 21:31:51
【问题描述】:
我需要生成这个 XML:
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos"
xmlns:bdo_fosfec="http://asocajas.app.com/example"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512>39756656</C512>
<C614>YAXMINNI</C614>
</registro82>
</registro54>
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512>79374740</C512>
<C614>VICTOR</C614>
</registro82>
</registro54>
</bdo_fosfec:RegistrosPagosElement>
我构建了这个 XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<bdo_fosfec:RegistrosPagosElement xsi:type="bdo_fosfec:RegistrosPagos" xmlns:bdo_fosfec="http://asocajas.app.com/example" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:apply-templates select="registro54"/>
</bdo_fosfec:RegistrosPagosElement>
</xsl:template>
<!--TEMPLATE REGISTRO 54-->
<xsl:template match="registro54">
<registro54 xsi:type="bdo_fosfec:Registro54">
<registro82 xsi:type="bdo_fosfec:Registro82">
<C512><xsl:value-of select="C512"/></C512>
<C614><xsl:value-of select="C614"/></C614>
</registro82>
</registro54>
</xsl:template>
</xsl:stylesheet>
但是,当我在 C# 上加载 XSLT 时,出现错误。
var xslt = new XslCompiledTransform();
xslt.Load(myxslt);
XSLT 编译错误。 'xsi' 是一个未声明的前缀。第 11 行,第 19 位。
好像第二个模板的“xsi”无法达到第一个模板的定义。如何修复我的 XSLT?
我对 XSLT 做了一些修改,但没有生成我想要的结果,正确的 xslt 设计是什么?
【问题讨论】:
-
尝试将
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"添加到xsl:stylesheet元素。 -
您需要声明要在 XSL 代码中使用的任何命名空间。由于您的 XSL 从未声明带有前缀
xsi的命名空间,因此 XSL 处理器无法处理它,因此会引发错误。 -
@EiríkrÚtlendi - 我认为如果你将我们的两个 cmets 结合起来,那将是一个不错的答案。如果您创建答案,我会很高兴地支持它。 :)
-
@DanielHaley rigth,这解决了我的错误,但结果 xml 不符合预期.. jumm... 无论如何,我会尝试一些事情。
-
@Makitodev - 如果您最终需要更多帮助,请创建一个新问题。