Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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
Java 将非结构化xml转换为结构化xml_Java_Xml_Xslt_Xslt 2.0 - Fatal编程技术网

Java 将非结构化xml转换为结构化xml

Java 将非结构化xml转换为结构化xml,java,xml,xslt,xslt-2.0,Java,Xml,Xslt,Xslt 2.0,我有一个非结构化的XML,它必须转换成结构化的。 我是从ApacheTika解析并由Parscit转换为xml的科学pdf文件中得到的。xml如下所示: 输入: <algorithm> <sectionHeader> Section1 </sectionHeader> <BodyText>Text goes here</BodyText> <sectionHeader> Section2 </s

我有一个非结构化的XML,它必须转换成结构化的。 我是从ApacheTika解析并由Parscit转换为xml的科学pdf文件中得到的。xml如下所示:

输入

<algorithm>
    <sectionHeader> Section1 </sectionHeader>
    <BodyText>Text goes here</BodyText>
    <sectionHeader> Section2 </sectionHeader>
    <BodyText>Text goes here</BodyText>
    <subsectionHeader>Subsection</subsectionHeader>
    <BodyText>Text goes here</BodyText>
    <sectionHeader> Section1 </sectionHeader>
    <BodyText>Text goes here</BodyText>
</algorithm>
<algorithm>
    <sectionHeader> 
        <Text> Section1 </Text>
        <BodyText>Text goes here</BodyText>
    </sectionHeader>
    <sectionHeader> 
       <Text> Section2 </Text>
       <BodyText>Text goes here</BodyText>
    <subsectionHeader>
        <Text>Subsection</Text>
        <BodyText>Text goes here</BodyText>
    </subsectionHeader>
</sectionHeader>
<sectionHeader> 
    <text>Section3 </Text>
    <BodyText>Text goes here</BodyText>
</sectionHeader>
</algorithm>

第一节
这里有文字
第2节
这里有文字
分段
这里有文字
第一节
这里有文字
输出

<algorithm>
    <sectionHeader> Section1 </sectionHeader>
    <BodyText>Text goes here</BodyText>
    <sectionHeader> Section2 </sectionHeader>
    <BodyText>Text goes here</BodyText>
    <subsectionHeader>Subsection</subsectionHeader>
    <BodyText>Text goes here</BodyText>
    <sectionHeader> Section1 </sectionHeader>
    <BodyText>Text goes here</BodyText>
</algorithm>
<algorithm>
    <sectionHeader> 
        <Text> Section1 </Text>
        <BodyText>Text goes here</BodyText>
    </sectionHeader>
    <sectionHeader> 
       <Text> Section2 </Text>
       <BodyText>Text goes here</BodyText>
    <subsectionHeader>
        <Text>Subsection</Text>
        <BodyText>Text goes here</BodyText>
    </subsectionHeader>
</sectionHeader>
<sectionHeader> 
    <text>Section3 </Text>
    <BodyText>Text goes here</BodyText>
</sectionHeader>
</algorithm>

第一节
这里有文字
第2节
这里有文字
分段
这里有文字
第三节
这里有文字

我可以在java中使用字符串生成器和xpath来实现这一点。但它会影响性能,因为我可能需要处理数百万个文档。xslt是一种更好的方法吗?

您可以这样做:

XSLT2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/algorithm">
    <xsl:copy>
        <xsl:for-each-group select="*" group-starting-with="sectionHeader">
            <sectionHeader> 
                <Text>
                    <xsl:value-of select="." />
                </Text>
                <xsl:for-each-group select="current-group()" group-starting-with="subsectionHeader">
                    <xsl:choose>
                        <xsl:when test="self::subsectionHeader">
                            <subsectionHeader> 
                                <Text>
                                    <xsl:value-of select="." />
                                </Text>
                                <xsl:copy-of select="current-group()[not(self::subsectionHeader)]"/>
                            </subsectionHeader> 
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:copy-of select="current-group()[not(self::sectionHeader)]"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each-group>
            </sectionHeader>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>


您对“更好”的定义是什么?您可以使用xslt做同样的事情吗?它可读性好还是不好?你的输入速度快还是慢?只有您可以尝试找到这些问题的答案。为什么结果中的
Section 2文本显示在此处
,而不是
Section 2文本显示在此处
?它甚至应该包装下面的subsectionHeader吗?哎呀!我的错误。已更正。请选择XSLT 1.0或2.0,而不是同时选择两者。有关XSLT 2.0示例,请参阅对类似问题从开始的每个组执行
,根据您有多少级别的节和子节,您的任务可能会更简单,只需为每个组包装两个
,或者通过识别构成当前级别标题元素的元素更难。