xslt - Explanation on why certain nodes show up -


all- new xslt. use understand basic.

the 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 elements -->      <xsl:template match="node()">         <xsl:copy>             <xsl:apply-templates select="node()"/>         </xsl:copy>     </xsl:template>      <!-- process first bullet text  element under template -->     <xsl:template match="bullettext[not(preceding-sibling::*[1][name()='bullettext'])]">         <!-- find element want stop @ -->         <xsl:variable name="stop" select="./following-sibling::*[name() != 'bullettext'][1]"/>         <list>             <xsl:choose>                 <!-- first, simple case:  there's no element have stop @ -->                 <xsl:when test="not($stop)">                     <xsl:apply-templates select="." mode="item"/>                     <xsl:apply-templates select="./following-sibling::bullettext" mode="item"/>                 </xsl:when>                  <!-- required -->                 <!-- transform elements between start , stop index 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  , 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 _ notice missing 'otherwise in choose.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform">      <xsl:strip-space elements="*"/>      <xsl:output indent="yes"/>        <!-- copy elements -->       <xsl:template match="node()">         <xsl:copy>             <xsl:apply-templates select="node()"/>         </xsl:copy>     </xsl:template>      <!-- process first bullet text  element under template -->     <xsl:template match="bullettext[not(preceding-sibling::*[1][name()='bullettext'])]">         <!-- find element want stop @ -->         <xsl:variable name="stop" select="./following-sibling::*[name() != 'bullettext'][1]"/>         <list>             <xsl:choose>                 <!-- first, simple case:  there's no element have stop @ -->                 <xsl:when test="not($stop)">                     <xsl:apply-templates select="." mode="item"/>                     <xsl:apply-templates select="./following-sibling::bullettext" mode="item"/>                 </xsl:when>                  <!-- required -->                 <!-- transform elements between start , stop index 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> 

both give same result

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

what value of otherwise in example. , why work without otherwise?

note: answer question posed. sorry not have link

your input not contain bullettext nodes. therefore, 3 templates matching bullettext never applied , makes no difference contain.

however, input such as:

<root>     <article>         <bullettext>alpha</bullettext>         <bullettext>bravo</bullettext>         <text>charlie</text>         <bullettext>delta</bullettext>     </article> </root> 

you see difference: first xslt return:

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

while second 1 produce only:

<?xml version="1.0" encoding="utf-8"?> <root>    <article>       <list/>       <text>charlie</text>       <list>          <item>delta</item>       </list>    </article> </root> 

Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -