Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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/6/opengl/4.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从第n个元素拆分为第x个元素_Java_Xml_Xslt_Xml Parsing_Xslt 1.0 - Fatal编程技术网

Java 将xml从第n个元素拆分为第x个元素

Java 将xml从第n个元素拆分为第x个元素,java,xml,xslt,xml-parsing,xslt-1.0,Java,Xml,Xslt,Xml Parsing,Xslt 1.0,下面是我的XML结构: <cars> <car> <ford color="black" >eco sport</ford> <maruti color="red" >zen</maruti> <hyundai color="blue" >accent</hyundai> </car> <car> &l

下面是我的XML结构:

<cars>  
  <car>  
    <ford color="black" >eco sport</ford>    
    <maruti color="red" >zen</maruti>  
    <hyundai color="blue" >accent</hyundai>  
  </car>  
  <car>  
    <ford color="green" >figo</ford >    
    <maruti color="red" >swift</maruti>  
    <hyundai color="white" >santro</hyundai>  
  </car> 
  <car>  
    <ford color="red" >aaa</ford >    
    <maruti color="red" >bbb</maruti>  
    <hyundai color="red" >ccc</hyundai>  
  <car>  
  </car>
    <ford color="white" >ddd</ford >    
    <maruti color="white" >eee</maruti>  
    <hyundai color="white" >fff</hyundai>  
  </car>
</cars>

有人能帮我吗?

你需要一个XML解析器。网上有很多资源。这是一篇关于读取和操作XML的文章。

您可以使用xslt,根据它可以将参数传递到所需的范围(传递方式取决于您使用的xslt处理器)。XSLT可以如下所示

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

    <!-- Take a start and end position to be output as a parameters from outside-->
    <xsl:param name="startPosition"  />
    <xsl:param name="endPosition"  />

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

    <!-- When you are processing car element... -->
    <xsl:template match="car">
        <!-- ... take a look at its position. Copy it only if its position is in the desired range -->
        <xsl:if test="not((position() &lt; $startPosition) or (position() &gt; $endPosition))">
            <xsl:copy>
                <xsl:apply-templates select="node() | @*" />
            </xsl:copy>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

当值startPosition=2和endPosition=3时,您将获得以下输出

<?xml version="1.0" encoding="UTF-8"?>
<cars>
    <car>
        <ford color="green">figo</ford>
        <maruti color="red">swift</maruti>
        <hyundai color="white">santro</hyundai>
    </car>
    <car>
        <ford color="red">aaa</ford>
        <maruti color="red">bbb</maruti>
        <hyundai color="red">ccc</hyundai>
    </car>
</cars>

菲戈
敏捷的
桑特罗
aaa
bbb
ccc

这只是一个概念。实际上,您应该检查参数的一些约束,例如它们是否是数字、endPosition是否不低于startPosition等。

在XSLT2.0中,您可以使用
subsequence()
。(我知道问题被标记为1.0,但这可能会对未来的访问者有所帮助。)

XML输入

<cars>  
    <car>  
        <ford color="black" >eco sport</ford>    
        <maruti color="red" >zen</maruti>  
        <hyundai color="blue" >accent</hyundai>  
    </car>  
    <car>  
        <ford color="green" >figo</ford >    
        <maruti color="red" >swift</maruti>  
        <hyundai color="white" >santro</hyundai>  
    </car> 
    <car>  
        <ford color="red" >aaa</ford >    
        <maruti color="red" >bbb</maruti>  
        <hyundai color="red" >ccc</hyundai>  
    </car>  
    <car>
        <ford color="white">ddd</ford>
        <maruti color="white">eee</maruti>
        <hyundai color="white">fff</hyundai>
    </car>
</cars>
<cars>
   <car>
      <ford color="green">figo</ford>
      <maruti color="red">swift</maruti>
      <hyundai color="white">santro</hyundai>
   </car>
   <car>
      <ford color="red">aaa</ford>
      <maruti color="red">bbb</maruti>
      <hyundai color="red">ccc</hyundai>
   </car>
</cars>

生态体育
禅
口音
菲戈
敏捷的
桑特罗
aaa
bbb
ccc
ddd
eee
fff
XSLT2.0

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="start" select="2"/>
    <xsl:param name="end" select="3"/>

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

    <xsl:template match="cars">
        <xsl:copy>
            <xsl:apply-templates select="@*|subsequence(car,$start,($end - $start)+1)"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

XML输出

<cars>  
    <car>  
        <ford color="black" >eco sport</ford>    
        <maruti color="red" >zen</maruti>  
        <hyundai color="blue" >accent</hyundai>  
    </car>  
    <car>  
        <ford color="green" >figo</ford >    
        <maruti color="red" >swift</maruti>  
        <hyundai color="white" >santro</hyundai>  
    </car> 
    <car>  
        <ford color="red" >aaa</ford >    
        <maruti color="red" >bbb</maruti>  
        <hyundai color="red" >ccc</hyundai>  
    </car>  
    <car>
        <ford color="white">ddd</ford>
        <maruti color="white">eee</maruti>
        <hyundai color="white">fff</hyundai>
    </car>
</cars>
<cars>
   <car>
      <ford color="green">figo</ford>
      <maruti color="red">swift</maruti>
      <hyundai color="white">santro</hyundai>
   </car>
   <car>
      <ford color="red">aaa</ford>
      <maruti color="red">bbb</maruti>
      <hyundai color="red">ccc</hyundai>
   </car>
</cars>

菲戈
敏捷的
桑特罗
aaa
bbb
ccc

我想你需要
position()
按预期工作,除非你使用像MSXML这样的XML解析器,默认情况下它会去除空白。我用Altova试过了,效果很好。但是,你是对的,你的建议会使解决方案更加可靠。