Advertisement
Guest User

XSLT + BEM

a guest
Jan 2nd, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 2.94 KB | None | 0 0
  1. <!-- XML -->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <?xml-stylesheet href="bem.xsl" type="text/xsl" ?>
  4. <root xmlns:m="bem">
  5. <b-gallery m:type="interiors" m:size="small">
  6.     <thumb>
  7.         <b-image src="" m:size="small"></b-image>
  8.     </thumb>
  9.     <thumb>
  10.         <b-image src="" m:size="small"></b-image>
  11.     </thumb>
  12.     <thumb>
  13.         <b-image src="" m:size="small"></b-image>
  14.     </thumb>
  15. </b-gallery>
  16. </root>
  17.  
  18. <!-- XSL -->
  19. <?xml version="1.0" encoding="utf-8"?>
  20. <xsl:stylesheet version="1.0"
  21.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  22.     xmlns:func="http://exslt.org/functions"
  23.     xmlns:exsl="http://exslt.org/common"
  24.     xmlns:bem="bem"
  25.     xmlns:m="bem:m"
  26.     extension-element-prefixes="func">
  27.  
  28. <xsl:output method="html" version="4.0" encoding="UTF-8"/>
  29.  
  30.     <xsl:template match="*" mode="bem">
  31.         <div>
  32.             <xsl:apply-templates select="." mode="m"/>
  33.             <xsl:apply-templates select="*" mode="bem"/>
  34.             <xsl:value-of select="text()"/>
  35.         </div>
  36.     </xsl:template>
  37.  
  38.     <xsl:template match="*" mode="m">
  39.         <xsl:copy-of select="@*[namespace-uri() = '' and name() != 'class']"/>
  40.         <xsl:choose>
  41.             <xsl:when test="starts-with(local-name(), 'b-')">
  42.                 <xsl:apply-templates select="." mode="b"/>
  43.             </xsl:when>
  44.             <xsl:otherwise>
  45.                 <xsl:apply-templates select="." mode="e"/>
  46.             </xsl:otherwise>
  47.         </xsl:choose>
  48.     </xsl:template>
  49.  
  50.     <xsl:template match="*" mode="b">
  51.         <xsl:attribute name="class">
  52.             <xsl:value-of select="local-name()"/>
  53.             <xsl:apply-templates select="@m:*">
  54.                 <xsl:with-param name="name" select="local-name()"/>
  55.             </xsl:apply-templates>
  56.         </xsl:attribute>
  57.     </xsl:template>
  58.  
  59.     <xsl:template match="*" mode="e">
  60.         <xsl:param name="name">
  61.             <xsl:variable name="ancestors" select="ancestor::*[starts-with(local-name(), 'b-')]"/>
  62.             <xsl:value-of select="local-name($ancestors[position() = last()])"/>
  63.             <xsl:text>__</xsl:text>
  64.             <xsl:value-of select="local-name()"/>
  65.         </xsl:param>
  66.  
  67.         <xsl:attribute name="class">
  68.             <xsl:value-of select="$name"/>
  69.             <xsl:apply-templates select="@m:*">
  70.                 <xsl:with-param name="name" select="$name"/>
  71.             </xsl:apply-templates>
  72.         </xsl:attribute>
  73.     </xsl:template>
  74.  
  75.     <xsl:template match="@m:*">
  76.         <xsl:param name="name"/>
  77.         <xsl:text> </xsl:text>
  78.         <xsl:value-of select="$name"/>
  79.         <xsl:text>_</xsl:text>
  80.         <xsl:value-of select="local-name()"/>_<xsl:value-of select="."/>
  81.     </xsl:template>
  82.  
  83.     <xsl:template match="@*"/>
  84.  
  85.     <func:function name="bem:block">
  86.         <xsl:param name="block"/>
  87.         <func:result>
  88.             <xsl:apply-templates select="exsl:node-set($block)" mode="bem"/>
  89.         </func:result>
  90.     </func:function>
  91. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement