Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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
Xml 使用XSLT或XQuery脚本重构和重新嵌套元素_Xml_Xpath_Xquery_Oxygenxml - Fatal编程技术网

Xml 使用XSLT或XQuery脚本重构和重新嵌套元素

Xml 使用XSLT或XQuery脚本重构和重新嵌套元素,xml,xpath,xquery,oxygenxml,Xml,Xpath,Xquery,Oxygenxml,我目前正在重构一批XML文档,这个过程包括将XML重新构造为新的修订DTD模式。由于使用了新的DTD,许多最初使用的元素要么重新调整用途,要么重新嵌套在其他元素中,要么全部删除。 下面的示例在根据DTD进行验证时是无效的xml文档。为了加快XML的重构过程,我认为XQuery脚本或XSLT转换可能会有所帮助。然而,我对这两种方法都没有经验,而且对XML还是相当陌生的。有人能给我解释一下,在重构这些文档时,XQuery、XSLT或Xpath最适合哪种语言吗 无效的XML: <PartsDoc

我目前正在重构一批XML文档,这个过程包括将XML重新构造为新的修订DTD模式。由于使用了新的DTD,许多最初使用的元素要么重新调整用途,要么重新嵌套在其他元素中,要么全部删除。 下面的示例在根据DTD进行验证时是无效的xml文档。为了加快XML的重构过程,我认为XQuery脚本或XSLT转换可能会有所帮助。然而,我对这两种方法都没有经验,而且对XML还是相当陌生的。有人能给我解释一下,在重构这些文档时,XQuery、XSLT或Xpath最适合哪种语言吗

无效的XML:

<PartsDoc foo=”” baa=”” bar=”” revno=”” docno=”” > 
    <PartsDocInfo>
        <repairlvl level=”shop” /> 
        <title id=”123”> Foo Electrical Control Box </title> 
    </PartsDocInfo> 

    <Parts.Category> 

    <figure id=”123” >
        <title id=”123”> Control Box Panels </title> 

    <subfig id=”123”>
            <graphic img=”foo.jpg” /> 
        </subfig>
    <!- - everything above is valid, the below portion is not - ->



<parts.item> 
            <callout id=”123”  config=”123” label=”1” /> 
            <mrs service=”shop” sc=”” mc=”” rec=”” /> 
            <nsn niin=”00-123-4567”> 4444-00-123-5467</nsn> 
            <cageno>12345</cageno>
            <partno>12345</partno>
            <name/>
            <desc id=”123” > Bolt 1/2inch </desc>
            <qty>4</qty>
 <parts.item>   
    </parts.category> 

电气控制箱
控制箱面板
4444-00-123-5467 
12345
12345
螺栓1/2英寸
4.
期望输出:

<PartsDoc foo=”” baa=”” bar=”” revno=”” docno=”” > 

        <PartsDocInfo>
        <repairlvl level=”shop” /> 
        <title id=”123”> Foo Electrical Control Box </title> 
    </PartsDocInfo> 
<Parts.Category> 
    <figure id=”123” >
        <title id=”123”> Control Box Panels </title> 
<subfig id=”123”>
          <graphic img=”foo.jpg” />
</subfig>
    <parts.item> 
        <callout id=”123”  config=”123” label=”1” /> 
<qty>4</qty>
<mrs service=”shop” sc=”” mc=”” rec=”” /> 
<nsn>
        <fsc>4444</fsc>
        <niin>00-12-5467
</nsn>
        <partno>12345</partno>
        <cageno>12345</cageno>
        <name/>
        <desc id=”123” > Bolt 1/2inch </desc>
    <parts.item>    
</parts.category> 

电气控制箱
控制箱面板
4.
4444
00-12-5467
12345
12345
螺栓1/2英寸
*请注意,
已移动 *注意
已移动 *注意
不包括已排序内容的子元素

此外,一些实例包括嵌套在
中的
元素作为子元素

<desc> 
    bolt 1/2inch
        <uoc>XYZ</uoc>
</desc>

螺栓1/2英寸
XYZ
其中
实际上应该在
之后和之前

<qty>


任何有关XSLT样式表或XQuery脚本的帮助,以及为什么选择一种语言而不是另一种语言的简短解释,都将不胜感激。我目前正在使用Oxygen 17 XML编辑器

当输出的大部分内容与输入内容相同时,XSLT通常更符合要求。一般原则是编写一个样式表,其中包含递归复制元素的一般规则,然后为要执行不同操作的元素添加规则

在XSLT 3.0中,一般规则是:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0">
  <xsl:mode on-no-match="shallow-copy"/>

  ... other code goes here ...
</xsl:transform>

... 其他代码在这里。。。
而在早期版本中,它是:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="*">
   <xsl:copy>
    <xsl:copy-of select="@*">
    <xsl:apply-templates/>
   </xsl:copy>
  </xsl:template>

  ... other code goes here ...
</xsl:transform>

... 其他代码在这里。。。
重新订购零件的模板规则。可以写入以下内容:

<xsl:template match="parts.item">
  <parts.item>
    <xsl:copy-of select="callout"/>
    <xsl:copy-of select="qty"/>
    <xsl:copy-of select="mrs"/>
    <nsn>
      <fsc><xsl:value-of select="substring-before(nsn, '-')"/></fsc>
      <niin><xsl:value-of select="nsn/@niin"/></niin>
    </nsn>
    <xsl:copy-of select="partno"/>
    <xsl:copy-of select="cageno"/>
    <xsl:copy-of select="name"/>
    <xsl:copy-of select="desc"/>
 </parts.item>

总而言之,以下XSLT 2.0样式表:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="parts.item">
        <parts.item>
            <xsl:copy-of select="callout"/>
            <xsl:copy-of select="qty"/>
            <xsl:copy-of select="mrs"/>
            <nsn>
                <fsc><xsl:value-of select="substring-before(nsn, '-')"/></fsc>
                <niin><xsl:value-of select="nsn/@niin"/></niin>
            </nsn>
            <xsl:copy-of select="partno"/>
            <xsl:copy-of select="cageno"/>
            <xsl:copy-of select="name"/>
            <xsl:copy-of select="desc"/>
        </parts.item>
    </xsl:template>
</xsl:transform>

应用于以下源文档:

<PartsDoc foo="" baa="" bar="" revno="" docno="" > 
    <PartsDocInfo>
        <repairlvl level="shop" /> 
        <title id="123"> Foo Electrical Control Box </title> 
    </PartsDocInfo> 

    <Parts.Category> 

        <figure id="123" >
        <title id="123"> Control Box Panels </title> 

         <subfig id="123">
                    <graphic img="foo.jpg" /> 
         </subfig>
                <!-- everything above is valid, the below portion is not -->

                <parts.item> 
                    <callout id="123"  config="123" label="1" /> 
                    <mrs service="shop" sc="" mc="" rec="" /> 
                    <nsn niin="00-123-4567"> 4444-00-123-5467</nsn> 
                    <cageno>12345</cageno>
                    <partno>12345</partno>
                    <name/>
                    <desc id="123" > Bolt 1/2inch </desc>
                    <qty>4</qty>
                </parts.item>
        </figure>
    </Parts.Category>
</PartsDoc>

电气控制箱
控制箱面板
4444-00-123-5467 
12345
12345
螺栓1/2英寸
4.
生成以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<PartsDoc foo="" baa="" bar="" revno="" docno="">
   <PartsDocInfo>
      <repairlvl level="shop"/>
      <title id="123"> Foo Electrical Control Box </title>
   </PartsDocInfo>
   <Parts.Category>
      <figure id="123">
         <title id="123"> Control Box Panels </title>
         <subfig id="123">
            <graphic img="foo.jpg"/>
         </subfig>
         <parts.item>
            <callout id="123" config="123" label="1"/>
            <qty>4</qty>
            <mrs service="shop" sc="" mc="" rec=""/>
            <nsn>
               <fsc> 4444</fsc>
               <niin>00-123-4567</niin>
            </nsn>
            <partno>12345</partno>
            <cageno>12345</cageno>
            <name/>
            <desc id="123"> Bolt 1/2inch </desc>
         </parts.item>
      </figure>
   </Parts.Category>
</PartsDoc>

电气控制箱
控制箱面板
4.
4444
00-123-4567
12345
12345
螺栓1/2英寸

当输出的大部分内容与输入内容相同时,XSLT通常更符合要求。一般原则是编写一个样式表,其中包含递归复制元素的一般规则,然后为要执行不同操作的元素添加规则

在XSLT 3.0中,一般规则是:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0">
  <xsl:mode on-no-match="shallow-copy"/>

  ... other code goes here ...
</xsl:transform>

... 其他代码在这里。。。
而在早期版本中,它是:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0">

  <xsl:template match="*">
   <xsl:copy>
    <xsl:copy-of select="@*">
    <xsl:apply-templates/>
   </xsl:copy>
  </xsl:template>

  ... other code goes here ...
</xsl:transform>

... 其他代码在这里。。。
重新订购零件的模板规则。可以写入以下内容:

<xsl:template match="parts.item">
  <parts.item>
    <xsl:copy-of select="callout"/>
    <xsl:copy-of select="qty"/>
    <xsl:copy-of select="mrs"/>
    <nsn>
      <fsc><xsl:value-of select="substring-before(nsn, '-')"/></fsc>
      <niin><xsl:value-of select="nsn/@niin"/></niin>
    </nsn>
    <xsl:copy-of select="partno"/>
    <xsl:copy-of select="cageno"/>
    <xsl:copy-of select="name"/>
    <xsl:copy-of select="desc"/>
 </parts.item>

总而言之,以下XSLT 2.0样式表:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="parts.item">
        <parts.item>
            <xsl:copy-of select="callout"/>
            <xsl:copy-of select="qty"/>
            <xsl:copy-of select="mrs"/>
            <nsn>
                <fsc><xsl:value-of select="substring-before(nsn, '-')"/></fsc>
                <niin><xsl:value-of select="nsn/@niin"/></niin>
            </nsn>
            <xsl:copy-of select="partno"/>
            <xsl:copy-of select="cageno"/>
            <xsl:copy-of select="name"/>
            <xsl:copy-of select="desc"/>
        </parts.item>
    </xsl:template>
</xsl:transform>

应用于以下源文档:

<PartsDoc foo="" baa="" bar="" revno="" docno="" > 
    <PartsDocInfo>
        <repairlvl level="shop" /> 
        <title id="123"> Foo Electrical Control Box </title> 
    </PartsDocInfo> 

    <Parts.Category> 

        <figure id="123" >
        <title id="123"> Control Box Panels </title> 

         <subfig id="123">
                    <graphic img="foo.jpg" /> 
         </subfig>
                <!-- everything above is valid, the below portion is not -->

                <parts.item> 
                    <callout id="123"  config="123" label="1" /> 
                    <mrs service="shop" sc="" mc="" rec="" /> 
                    <nsn niin="00-123-4567"> 4444-00-123-5467</nsn> 
                    <cageno>12345</cageno>
                    <partno>12345</partno>
                    <name/>
                    <desc id="123" > Bolt 1/2inch </desc>
                    <qty>4</qty>
                </parts.item>
        </figure>
    </Parts.Category>
</PartsDoc>

电气控制箱
控制箱面板
4444-00-123-5467 
12345
12345
螺栓1/2英寸
4.
生成以下输出:

<?xml version="1.0" encoding="UTF-8"?>
<PartsDoc foo="" baa="" bar="" revno="" docno="">
   <PartsDocInfo>
      <repairlvl level="shop"/>
      <title id="123"> Foo Electrical Control Box </title>
   </PartsDocInfo>
   <Parts.Category>
      <figure id="123">
         <title id="123"> Control Box Panels </title>
         <subfig id="123">
            <graphic img="foo.jpg"/>
         </subfig>
         <parts.item>
            <callout id="123" config="123" label="1"/>
            <qty>4</qty>
            <mrs service="shop" sc="" mc="" rec=""/>
            <nsn>
               <fsc> 4444</fsc>
               <niin>00-123-4567</niin>
            </nsn>
            <partno>12345</partno>
            <cageno>12345</cageno>
            <name/>
            <desc id="123"> Bolt 1/2inch </desc>
         </parts.item>
      </figure>
   </Parts.Category>
</PartsDoc>

电气控制箱
控制箱面板
4.
4444
00-123-4567
12345
12345
螺栓1/2英寸

然后你做错了什么。在您提供的XML和我的XSLT中都有一些小的输入错误,但在修复这些错误后,它会按预期工作,我已经添加了完整的细节。Michael如果我想在parts.item元素中保留任何属性,我该怎么做?是否需要通过使用@*运算符进行进一步筛选来更改