Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设计模板XSLT以生成XML_Xml_Xslt - Fatal编程技术网

设计模板XSLT以生成XML

设计模板XSLT以生成XML,xml,xslt,Xml,Xslt,我需要生成包含所有内容、每个节点上的名称空间等的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_fosfe

我需要生成包含所有内容、每个节点上的名称空间等的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>
但当我用XSLT转换myxml时,结果xml并不像预期的那样

结果:

myxml

我不知道我做错了什么,我尝试过删除样式表的名称空间xmlns:xsi=xsi:type=bdo_fosfec:RegistersPages xmlns:bdo_fosfec=http://asocajas.hp.com/bdo_fosfec,但它会产生错误,我也尝试过不使用模板,结果接近期望的结果,但与我希望的结果不同


Thnks

看起来您对上下文有点困惑,这导致XPath选择器出现故障

第一个问题 你从这里开始:

<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>
因此,我们的上下文是registro54元素。我们尝试使用以下两种语句获取值:

            <C512><xsl:value-of select="C512"/></C512>
            <C614><xsl:value-of select="C614"/></C614>
。。。我们看到C512和C614元素是registro82的子元素。因此,要实际获取这些值,请将select语句更改为:

            <C512><xsl:value-of select="registro82/C512"/></C512>
            <C614><xsl:value-of select="registro82/C614"/></C614>
结论
请记住,使用相对XPath选择任何不以/、开头的XPath表达式,逻辑根将仅从上下文元素的起点进行选择。

您针对第一个问题建议的修复方案无效,因为输入xml的根元素不在命名空间中,因此XPath将不匹配/选择任何内容。您对@DanielHaley,我已经编辑了您的答案,以适合第一个问题和第二个问题的myxml。谢谢你的回答,这对我帮助很大。是的,你是对的,我在XSLT文件中遇到了几个问题。哈,你的回答也是正确的。谢谢大家!@DanielHaley-谢谢你的精神检查。我使用Saxon HE 9.6.0.7在OxygenXML中对其进行了测试,更改select效果很好——但是在各种中断之间,我忘了提到在更改匹配之前必须移动bdo_fosfec:前缀的名称空间声明。我将立即进行编辑。@EiríkrÚtlendi-问题中输入myxml的根元素实际上是bdo_fosfec_x003A_registorspagoselement请注意,根本没有名称空间前缀。听起来OP已经搞定了,不过没什么大不了的+1@Makitodev,为我之前的困惑道歉。您提出了一个编辑来修复Daniel刚才评论的内容,而我在查看您的编辑时引用了错误的XML—我错误地查看了所需的输出,而不是myxml输入。我会修好我的帖子。
<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>
            <C512><xsl:value-of select="C512"/></C512>
            <C614><xsl:value-of select="C614"/></C614>
<registro54 xsi:type="bdo_fosfec:Registro54">
    <registro82 xsi:type="bdo_fosfec:Registro82">
        <C512>39756656</C512>
        <C614>YAXMINNI</C614>
    </registro82>
</registro54>
            <C512><xsl:value-of select="registro82/C512"/></C512>
            <C614><xsl:value-of select="registro82/C614"/></C614>