2015-04-22 58 views
0

全部 - XSLT非常新穎。我可以使用一些幫助來理解一些基本的。爲什麼某些節點出現的解釋

的XML

<root> 
     <Article> 
      <Bullettext>10,00 </Bullettext> 
      <Bullettext>8,00 </Bullettext> 

     </Article> 
     <Article> 
      <something>some text</something> 
     </Article> 
     <Article> 
      <Corpsdetexte>Bulgaria</Corpsdetexte> 

      <Bullettext>15,0 </Bullettext> 
      <Bullettext>10,0 </Bullettext> 

     </Article> 
     <Article> 
      <Corpsdetexte>Somaialia</Corpsdetexte> 
      <bunk>Test</bunk> 

      <Bullettext>15,1</Bullettext> 
      <Bullettext>10,2</Bullettext> 
      <Bullettext>20,3</Bullettext> 
      <Bullettext>25,4</Bullettext> 
      <Bullettext>30,5 </Bullettext> 

     </Article> 
    </root> 

XSLT 1

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

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

    <xsl:output indent="yes"/> 



    <!-- copy all elements as is --> 

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

    <!-- process only the first bullet text element under this template --> 
    <xsl:template match="Bullettext[not(preceding-sibling::*[1][name()='Bullettext'])]"> 
     <!-- find the element that we want to stop at --> 
     <xsl:variable name="stop" select="./following-sibling::*[name() != 'Bullettext'][1]"/> 
     <LIST> 
      <xsl:choose> 
       <!-- first, the simple case: there's no element we have to stop at --> 
       <xsl:when test="not($stop)"> 
        <xsl:apply-templates select="." mode="item"/> 
        <xsl:apply-templates select="./following-sibling::Bullettext" mode="item"/> 
       </xsl:when> 

       <!-- is this required --> 
       <!-- transform all elements between the start and stop index into items --> 
       <xsl:otherwise> 
        <xsl:variable name="start_index" select="count(preceding-sibling::*) + 1"/> 
        <xsl:variable name="stop_index" select="count($stop/preceding-sibling::*)"/> 
        <xsl:apply-templates select="../*[position() &gt;= $start_index 
and position() &lt;= $stop_index]" 
mode="item"/> 
       </xsl:otherwise> 
      </xsl:choose> 
     </LIST> 
    </xsl:template> 

    <xsl:template match="Bullettext" /> 


    <xsl:template match="Bullettext" mode="item"> 
     <ITEM> 
      <xsl:value-of select="."/> 
     </ITEM> 
    </xsl:template> 


</xsl:stylesheet> 

XSLT 2 _注意失蹤「,否則的選擇。

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

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

    <xsl:output indent="yes"/> 



    <!-- copy all elements as is --> 


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

    <!-- process only the first bullet text element under this template --> 
    <xsl:template match="Bullettext[not(preceding-sibling::*[1][name()='Bullettext'])]"> 
     <!-- find the element that we want to stop at --> 
     <xsl:variable name="stop" select="./following-sibling::*[name() != 'Bullettext'][1]"/> 
     <LIST> 
      <xsl:choose> 
       <!-- first, the simple case: there's no element we have to stop at --> 
       <xsl:when test="not($stop)"> 
        <xsl:apply-templates select="." mode="item"/> 
        <xsl:apply-templates select="./following-sibling::Bullettext" mode="item"/> 
       </xsl:when> 

       <!-- is this required --> 
       <!-- transform all elements between the start and stop index into items --> 
      </xsl:choose> 
     </LIST> 
    </xsl:template> 

    <xsl:template match="Bullettext" /> 


    <xsl:template match="Bullettext" mode="item"> 
     <ITEM> 
      <xsl:value-of select="."/> 
     </ITEM> 
    </xsl:template> 


</xsl:stylesheet> 

都給予同樣的結果

<root> 
    <Article> 
     <LIST> 
      <ITEM>10,00 </ITEM> 
      <ITEM>8,00 </ITEM> 
     </LIST> 
    </Article> 
    <Article> 
     <something>some text</something> 
    </Article> 
    <Article> 
     <Corpsdetexte>Bulgaria</Corpsdetexte> 
     <LIST> 
      <ITEM>15,0 </ITEM> 
      <ITEM>10,0 </ITEM> 
     </LIST> 
    </Article> 
    <Article> 
     <Corpsdetexte>Somaialia</Corpsdetexte> 
     <bunk>Test</bunk> 
     <LIST> 
      <ITEM>15,1 </ITEM> 
      <ITEM>10,2 </ITEM> 
      <ITEM>20,3 </ITEM> 
      <ITEM>25,4 </ITEM> 
      <ITEM>30,5 </ITEM> 
     </LIST> 
    </Article> 
</root> 

是什麼,否則本例中的價值。爲什麼它沒有其他的工作呢?

注意:這是提出的問題的答案。抱歉,沒有鏈接

+0

輸入和輸出之間有什麼區別? –

+0

@Mathias - 我的粗心。抱歉!我更新了源XML。目標是將Bullettext節點包裹在列表節點的周圍,並將節點名稱從Bullettext更改爲項目 – StillStumbling

+0

@Mathias - 看起來像out將是不同的。任何幫助瞭解第一個xslt的流程將會很有幫助。 – StillStumbling

回答

1

您的輸入不包含任何Bullettext節點。因此,匹配Bullettext的三個模板從不應用,它們包含的內容沒有區別。

然而,隨着輸入如:

<root> 
    <Article> 
     <Bullettext>alpha</Bullettext> 
     <Bullettext>bravo</Bullettext> 
     <text>charlie</text> 
     <Bullettext>delta</Bullettext> 
    </Article> 
</root> 

,你會看到一個區別:第一XSLT將返回:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <Article> 
     <LIST> 
     <ITEM>alpha</ITEM> 
     <ITEM>bravo</ITEM> 
     </LIST> 
     <text>charlie</text> 
     <LIST> 
     <ITEM>delta</ITEM> 
     </LIST> 
    </Article> 
</root> 

,而第二個只會產生:

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <Article> 
     <LIST/> 
     <text>charlie</text> 
     <LIST> 
     <ITEM>delta</ITEM> 
     </LIST> 
    </Article> 
</root> 
+0

@micahel hor257k是的,你是對的,我更新了源XML。我確信我正在獲得產出。你能解釋第一個XSLT中發生了什麼嗎?當'when'部分正在執行時和其他時候。我正在努力理解這一點。 – StillStumbling

+1

@StillStumbling你需要一個例子,其中「Bullettext」節點與其他節點作爲同胞散佈。你有什麼是「兄弟遞歸」的例子,如果你願意的話,它是一種替代形式的分組。 –

相關問題