xml - XSLT - Set attribute with value of child -


i want set attribute of parent based on value of first child. using xslt 1.0.

<div>     <div>         <span>a</span>         <span>text...text</span>     </div>     <div>         <span>1</span>         <span>text...text</span>     </div> </div> 

should transformed to:

<div>     <div data-type="alphanumeric">         <span>text...text</span>     </div>     <div data-type="numeric">         <span>text...text</span>     </div> </div> 

can me how can this?

thank you!

try like:

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/> <xsl:strip-space elements="*"/>  <!-- identity transform --> <xsl:template match="@*|node()">     <xsl:copy>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template>  <xsl:template match="div[span]">     <xsl:copy>         <xsl:attribute name="data-type">             <xsl:choose>                 <xsl:when test="translate(span[1], '0123456789', '')">alphanumeric</xsl:when>                 <xsl:otherwise>numeric</xsl:otherwise>             </xsl:choose>         </xsl:attribute>         <xsl:apply-templates select="@*|node()"/>     </xsl:copy> </xsl:template>  </xsl:stylesheet> 

note selects numeric when value contains digits only; iow, value of <span>0.1</span> tagged alphanumeric.

alternatively, use:

<xsl:when test="number(span[1])=number(span[1])">numeric</xsl:when> <xsl:otherwise>alphanumeric</xsl:otherwise> 

which tag number numeric.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -