设计模板 XSLT 以生成 XML [英] Design Template XSLT to generate XML

查看:32
本文介绍了设计模板 XSLT 以生成 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用所有东西、每个节点上的命名空间等生成这个 xml

i need to generate this xml with all things, namespaces on each node etc

<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

And i build a this 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 不是预期的

but when i transform myxml with the XSLT the result xml is not as expected

结果:

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

我不知道我做错了什么,我试过删除 nameSpace xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: type = "bdo_fosfec: RegistersPages" xmlns: bdo_fosfec = "http://asocajas.hp.com/bdo_fosfec "的样式表,但它产生错误,我也尝试不使用模板",结果接近所需的但它与我希望的不一样

I do not know what I'm doing wrong, I've tried removing the nameSpace xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: type = "bdo_fosfec: RegistersPages" xmlns: bdo_fosfec = "http: //asocajas.hp.com/bdo_fosfec "of the stylesheet but it generates error, I also tried not to use" templates "and the result approaches the desired one but it is not the same that I hope

感谢

推荐答案

您似乎对上下文有点困惑,这会导致 XPath 选择器出错.

It looks like you're getting a bit confused about context, and this is leading to faulty XPath selectors.

你从这里开始:

<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 语句什么都不选.

So you matched on the logical root element. Within the context of this logical root element, you use xsl:apply-templates -- but with select="registro54". This XPath is looking for any registro54 elements that are immediate children of the context element. However, the immediate child of the logical root is the topmost element in the file, in your case, bdo_fosfec_x003A_RegistrosPagosElement. So this select statement selects nothing.

我们可以通过改变两件事之一来解决这个问题.要么:

We can fix this by changing one of two things. Either:

  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".
  1. Change your xsl:template match statement to xsl:template match="/bdo_fosfec_x003A_RegistrosPagosElement",
    or
  2. Change your xsl:apply-templates select statement to xsl:apply-templates select="bdo_fosfec_x003A_RegistrosPagosElement/registro54".

第二个问题

即使在实施了第一个修复程序之后,我们也没有得到我们需要的东西.您的第二个模板:

Second problem

Even after implementing this first fix, we don't get what we need. Your second template:

<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 元素.我们尝试使用这两个语句获取值:

So our context is the registro54 element. We try to get values using these two statements:

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

这些不会产生任何结果,同样是因为 XPath 没有选择任何内容.在 registro54 的上下文中,这些 select 语句尝试查找 registro54 的名为 C512 或 <代码>C614.

These produce nothing, again because the XPaths select nothing. Within the context of registro54, these select statements try to find immediate children of registro54 that are named C512 or C614.

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

However, if we look at your input XML:

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

... 我们看到 C512C614 元素是 registro82 的子元素.因此,要实际获取这些值,请将 select 语句更改为:

... we see that the C512 and C614 elements are children of registro82 instead. So to actually get these values, change your select statements to:

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

结论

请记住,select 和相对 XPath(任何不以 / 开头的 XPath 表达式,逻辑根)只会从上下文的起点进行选择元素.

Conclusion

Remember that select with relative XPaths (any XPath expression that doesn't start with /, the logical root) will only ever select from the starting point of the context element.

这篇关于设计模板 XSLT 以生成 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆