Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/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
如何使用XSL 2.0在xml文件中的两个特定元素之间选择多个元素_Xml_Xslt - Fatal编程技术网

如何使用XSL 2.0在xml文件中的两个特定元素之间选择多个元素

如何使用XSL 2.0在xml文件中的两个特定元素之间选择多个元素,xml,xslt,Xml,Xslt,我需要使用XSL提取xml中两个元素之间的几个元素,并将提取的内容写入xml文件 我有下面的XML <?xml version="1.0" encoding="UTF-8"?> <root> <element_1> <h1>title</h1> <p>paragraph</p> <p>paragraph</p> <

我需要使用XSL提取xml中两个元素之间的几个元素,并将提取的内容写入xml文件

我有下面的XML

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
    <element_1>
      <h1>title</h1>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <table>
         <tr>
            <td />
         </tr>
      </table>
      <h1>Another Title</h1>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <table>
         <tr>
            <td/>
         </tr>
      </table>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <table>
         <tr>
            <td/>
         </tr>
      </table>
      <h1>Some other Title</h1>
      <p>paragraph</p>
      <table>
         <tr>
            <td/>
         </tr>
      </table>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
      <table>
         <tr>
            <td/>
         </tr>
      </table>
      <p>paragraph</p>
      <p>paragraph</p>
      <p>paragraph</p>
   </element_1>
   <element_2/>
</root>

标题
段落

段落

段落

段落

另一个标题 段落

段落

段落

段落

段落

段落

段落

段落

其他头衔 段落

段落

段落

段落

段落

段落

段落

段落

XSL对我来说是新手,在生成包含提取内容的新文件时面临困难。不知何故,我可以管理生成新文件,但无法提取两个特定()标记之间的标记。上面的XML是第三方工具的输出

请分享您的想法,或者如果有人有想法提取标签之间的元素

预期输出应如下所示:

File1.xml:

<modified>
    <h1>title</h1>
    <p>paragraph</p>
    <p>paragraph</p>
    <p>paragraph</p>
    <p>paragraph</p>
    <table><tr><td/></tr></table>
</modified>

标题
段落

段落

段落

段落

File2.xml:

    <modified>
        <h1>Another Title</h1>
        <p>paragraph</p>
        <p>paragraph</p>
        <p>paragraph</p>
        <p>paragraph</p>
        <table><tr><td/></tr></table>
        <p>paragraph</p>
        <p>paragraph</p>
        <p>paragraph</p>
        <p>paragraph</p>
        <table><tr><td/></tr></table>
    </modified>

另一个标题
段落

段落

段落

段落

段落

段落

段落

段落

File3.xml:

<modified>
    <p>paragraph</p>
    <table><tr><td/></tr></table>
    <p>paragraph</p>
    <p>paragraph</p>
    <p>paragraph</p>
    <p>paragraph</p>
    <table><tr><td/></tr></table>
    <p>paragraph</p>
    <p>paragraph</p>
    <p>paragraph</p>
</modified>

段落

段落

段落

段落

段落

段落

段落

段落


谢谢。

您可以使用以下样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

    <xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="element_1">
        <xsl:for-each-group select="*" group-starting-with="h1">
            <xsl:result-document href="{concat('File', count(preceding-sibling::h1) + 1, '.xml')}">
                <modified>
                    <xsl:apply-templates select="current-group()"/>
                </modified>
            </xsl:result-document>
        </xsl:for-each-group>
    </xsl:template>    

</xsl:stylesheet>


它将以
h1
开头的节点分组,并将其输出到文件名
文件[no].xml
中。使用
应足以为第一个组生成
File1.xml
,为第二个组生成
File2.xml
,依此类推。感谢您的响应@Joel M.Lamsen但是当我在我的系统中尝试XSL时,我得到了以下错误:致命错误:java.lang.IllegalArgumentException:URI方案不是“文件”;系统ID:file:///C:/projects/XSL/XSL_Transformation.xsl; 第#行:8;列#:-1转换过程中出现错误net.sf.saxon.trans.XPathException:java.lang.IllegalArgumentException:URI方案不是“文件”@kepler,您能告诉我们在出现错误时您是如何运行saxon的吗?@MartinHonnen我正在使用Eclipse并将saxon jar添加到我的项目库中。下面是我图书馆里的罐子。