2013-08-19 41 views
1

下面是我的XML結構:拆分XML來第x個元素

<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結構,我需要在Java解析器將拆分XML和只有2只返回一個XML或3個元素(如從1-2或2-3或2-4),因爲我將動態指定元素行。所以,如果我通過PARAMS的方法splitXML(2,3),我回到新的XML應該是這樣的:

<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> 

可能一些身體幫我嗎?

回答

1

您可以使用基於Identity transform 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和終端位置= 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> 

這僅僅是一個概念。實際上,如果endPosition不低於startPosition,則應該檢查一些參數約束,如果它們是數字。

+1

我認爲你需要''或''讓'position()'按預期工作,除非你使用默認剝離空白空間的XML解析器,如MSXML。 –

+0

我嘗試了Altova,它工作。但是,是的,你說得對,這個解決方案在你的建議中會更強大。 –

2

在XSLT 2.0中,您可以使用subsequence()。 (我知道這個問題是標籤1.0,但也許這將有助於未來的遊客。)

XML輸入

XSLT 2.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="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> 
相關問題