设计模板XSLT生成XML(Design Template XSLT to generate XML

2019-09-29 04:11发布

我需要生成该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>

但是当我变换myxml与XSLT并不如预期的结果XML

结果

 <?xml version="1.0" encoding="utf-8"?>
   <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" />

myxml

 <?xml version="1.0" encoding="utf-8"?>
  <bdo_fosfec_x003A_RegistrosPagosElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <registro54>
     <registro82>
      <C512>123456789</C512>
      <C614>Miguel</C614>
     </registro82>
    </registro54>
    <registro54>
     <registro82>
      <C512>1234567890</C512>
      <C614>Jerónimo</C614>
     </registro82>
    </registro54>
  </bdo_fosfec_x003A_RegistrosPagosElement>

我不知道我做错了,我已经试过删除命名空间的xmlns:XSI =“ http://www.w3.org/2001/XMLSchema-instance ”的xsi:type =“bdo_fosfec:RegistersPages”的xmlns: bdo_fosfec =“HTTP://asocajas.hp.com/bdo_fosfec‘的样式表,但它产生的错误,我也尽量不使用’模板”,结果达到所希望的一个,但它是不一样的,我希望

Thnks

Answer 1:

它看起来像你得到有关背景有点迷糊了,这是导致故障的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>

所以,你match编逻辑根元素。 在这个逻辑根元素的情况下,可以使用xsl:apply-templates -但select="registro54" 。 这个XPath正在寻找任何registro54是上下文元素的直接子元素。 然而,逻辑根的直接子是文件中最顶端的元素,你的情况, bdo_fosfec_x003A_RegistrosPagosElement 。 所以这个select语句选择什么。

我们可以通过改变两种情况之一解决此问题。 或者:

  1. 改变你xsl:template match语句xsl:template match="/bdo_fosfec_x003A_RegistrosPagosElement"
    要么
  2. 改变你的xsl:apply-templates select语句xsl:apply-templates select="bdo_fosfec_x003A_RegistrosPagosElement/registro54"

第二个问题

即使实现这个第一修复程序后,我们没有得到我们所需要的。 你的第二个模板:

<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>

因此,我们的上下文是registro54元素。 我们尝试使用这两个语句来获取值:

            <C512><xsl:value-of select="C512"/></C512>
            <C614><xsl:value-of select="C614"/></C614>

这些生产什么,再次因为XPath的选择罢了。 在上下文registro54 ,这些select语句试图找到的直接子registro54被命名为C512C614

但是,如果我们看看你的XML输入:

<registro54 xsi:type="bdo_fosfec:Registro54">
    <registro82 xsi:type="bdo_fosfec:Registro82">
        <C512>39756656</C512>
        <C614>YAXMINNI</C614>
    </registro82>
</registro54>

......我们看到, C512C614元素的儿童registro82代替。 所以真正得到这些值,改变你的select语句:

            <C512><xsl:value-of select="registro82/C512"/></C512>
            <C614><xsl:value-of select="registro82/C614"/></C614>

结论

请记住, select与相对的XPath(任何XPath表达式不与启动/ ,逻辑根)将只从上下文元件的起点选择。



文章来源: Design Template XSLT to generate XML
标签: xml xslt