XSL-T 将输入 xml 中缺少的架构元素添加为空标签 [英] XSL-T Adding Missing schema elements from input xml as empty tags

查看:16
本文介绍了XSL-T 将输入 xml 中缺少的架构元素添加为空标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XSL-T 比较陌生.我的要求比较简单.我想将 xml 中不存在的 Schema 缺失元素添加为空标签.

I am relatively new to XSL-T. My requirement is rather simple. I want to add missing elements of Schema which are not present in the xml as empty tags.

例如

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           attributeFormDefault="unqualified"
           elementFormDefault="qualified">
  <xs:element name="RootElement">
      <xs:complexType>
         <xs:sequence>
            <xs:element name="XMLTagOne">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element type="xs:string" name="value1"/>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
            <xs:element name="RepeatableElementOne" maxOccurs="unbounded" minOccurs="0">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element type="xs:string" name="value2"/>
                     <xs:element name="RepeatableElemenTwo" maxOccurs="unbounded" minOccurs="0">
                        <xs:complexType>
                           <xs:sequence>
                              <xs:element type="xs:string" name="value3"/>
                           </xs:sequence>
                        </xs:complexType>
                     </xs:element>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:sequence>
      </xs:complexType>
  </xs:element>
</xs:schema>

考虑这个 i/p:

<RootElement>
    <RepeatableElementOne>
        <value2>bb</value2>
        <RepeatableElemenTwo>
            <value3>cc</value3>
        </RepeatableElemenTwo>
        <RepeatableElemenTwo>
            <value3>dd</value3>
        </RepeatableElemenTwo>
    </RepeatableElementOne>
    <RepeatableElementOne>
        <value2>ee</value2>
    </RepeatableElementOne>
</RootElement>

对于这个 i/p,我希望元素 作为空标签添加.

For this i/p I want the element <XMLTagOne> and <RepeatableElemenTwo> to be added as empty tags.

预期 O/P:

<RootElement>
    <XMLTagOne>               <!-- Added as empty tag though not present in i/p-->
        <value1></value1>
    </XMLTagOne>
    <RepeatableElementOne>
        <value2>bb</value2>
        <RepeatableElemenTwo>
            <value3>cc</value3>
        </RepeatableElemenTwo>
        <RepeatableElemenTwo>
            <value3>dd</value3>
        </RepeatableElemenTwo>
    </RepeatableElementOne>
    <RepeatableElementOne>
        <value2>ee</value2>
        <RepeatableElemenTwo>
            <value3></value3>
        </RepeatableElemenTwo>
    </RepeatableElementOne>
</RootElement>

通过一些初步研究,我发现我必须使用匹配所有元素的身份模板遍历每个节点.你能建议我如何解决这个问题吗?谢谢.

With Some Initial Research I discovered that I have to traverse through every node with an identity template matching all elements. Can you suggest me how I can approach this problem? Thanks.

编辑

我的设计方法:

  1. 基于 xsd 创建一个中间 xml 文档.

像这样,

<Root>
<a></a>
<b></b>
<Root>

  • 遍历所有单个节点.(身份模板 ??)
  • 从我手头的源 XML 中获取每个节点的值.
    • Traverse through all the individual nodes. (Identity Template ??)
    • Get the value of each node from the source XML which I have in-hand.
    • 这种方法的问题

      • 重复元素.
      • 必须检查计数是否大于 1.如果是,则使用 处理源文档中的节点.
      • Repeating Elements.
      • Will have to check if the count is > 1. If so, then use <xsl:for-each> to process the nodes from the source document.

      推荐答案

      本质上你自己设置的任务是编写一个模式处理器,不仅验证源文档(以任何模式处理器的方式),而且修复如果它们无效.我认为你对这项任务的重要性一无所知.通用解决方案将涉及构建与模式中每个复杂类型定义所定义的语法相对应的有限状态机,验证实例是否使用此有限状态机,然后在发现与任何路径都不匹配的元素时调用修复功能在 FSM 中.如果你限制自己提供缺失的元素,那么当你在状态 S 中遇到元素 E 并且 E 上没有从 S 的转换时,实际上并不难检测到,那么可能有一个元素 F 从S,这会将您带到在 E 上有转换的状态 S2.但即使您发现了这一点,您仍然面临更多挑战:您可能必须在 F、G 和 H 之间进行选择,这些都可以修复"" 元素;您可能会发现需要插入多个元素来进行修复(这开始涉及一些复杂的图形搜索);并且一旦找到要插入的元素,就必须构造该元素的一个实例,其内容是有效的.

      Essentially the task you are setting yourself is to write a schema processor that not only validates source documents (in the way that any schema processor does), but also repairs them if they are invalid. I don't think you have any idea of the magnitude of this task. A general solution would involve building a finite state machine corresponding to the grammar defined by each complex type definition in the schema, validating instances against using this finite state machine, and then invoking the repair functionality when an element is found that does not match any path in the FSM. If you restrict yourself to supplying missing elements, then it's not actually too hard to detect that when you encounter element E in state S, and there is no transition on E from S, then there might be an element F that does have a transition from S, which leads you to a state S2 that has a transition on E. But even if you find that, you still have a couple more challenges: you might have to choose between F, G, and H which would all be possible "repair" elements; you might find that you need to insert more than one element to make a repair (which starts to involve some complex graph-searching); and once you have found an element that you want to insert, you have to construct an instance of that element whose content is valid.

      这将是一个很好的博士项目.

      It would make a good PhD project.

      这篇关于XSL-T 将输入 xml 中缺少的架构元素添加为空标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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