xml - Multiple choice values and more than 1 restriction -


got xml file:

<?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="equipos.xsl"?> <equipos>     <equipo nombre="los paellas" personas="2"/>     <equipo nombre="los arrocitos" personas="13"/>     <equipo nombre="los gambas" personas="6"/>     <equipo nombre="los mejillones" personas="3"/>     <equipo nombre="los garrofones" personas="17"/>     <equipo nombre="los limones" personas="7"/> </equipos> 

applying xslt output must be:

  • attribute "personas" overrided attribute called "categoria"
  • "categoria" must have valor 1 if personas < 5
  • "categoria" must have valor 2 if personas between 5 , 10
  • "categoria" must have valor 3 if personas > 10

this xslt now, dont find way 3rd condition "categoria" on choose...

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">  <xsl:template match="equipos">     <xsl:copy>         <xsl:apply-templates/>     </xsl:copy> </xsl:template>  <xsl:template match="equipo">     <xsl:copy>         <xsl:attribute name="nombre">             <xsl:value-of select="@nombre"/>         </xsl:attribute>         <xsl:attribute name="categoria">             <xsl:choose>                 <xsl:when test="@personas &lt; 5">                     <xsl:text>1</xsl:text>                 </xsl:when>                 <xsl:otherwise>                     <xsl:text>2</xsl:text>                 </xsl:otherwise>             </xsl:choose>         </xsl:attribute>     </xsl:copy> </xsl:template>  </xsl:stylesheet> 

you can have many when elements inside choose:

<xsl:choose>     <xsl:when test="@personas &lt; 5">         <xsl:text>1</xsl:text>     </xsl:when>     <xsl:when test="@personas &lt;= 10">         <xsl:text>2</xsl:text>     </xsl:when>     <xsl:otherwise>         <xsl:text>3</xsl:text>     </xsl:otherwise> </xsl:choose> 

a choose takes first matching when branch, don't need check >=5 in second branch - know because didn't take first one.

but future reference, more idiomatic xslt way approach might use matching templates instead of choose construct:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">    <!-- copy unchanged except when overridden -->   <xsl:template match="@*|node()">     <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>   </xsl:template>    <xsl:template match="@personas[. &lt; 5]" priority="10">     <xsl:attribute name="categoria">1</xsl:attribute>   </xsl:template>    <xsl:template match="@personas[. &lt;= 10]" priority="9">     <xsl:attribute name="categoria">2</xsl:attribute>   </xsl:template>    <xsl:template match="@personas" priority="8">     <xsl:attribute name="categoria">3</xsl:attribute>   </xsl:template> </xsl:stylesheet> 

here need explicit priorities because patterns @personas[. &lt; 5] , @personas[. &lt;= 10] considered equally specific default.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -