Advertisement
Guest User

BEM on XSLT

a guest
Jan 4th, 2013
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 3.30 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" style="width:100%">
  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. <b-search m:type="forum-search" m:orientation="hotrizontal">
  17.     <field m:type="flat">
  18.     search field
  19.         <some-element>
  20.             inside some element
  21.         </some-element>
  22.     </field>
  23.     <button m:type="flat" m:label="text">
  24.         search
  25.     </button>
  26. </b-search>
  27. </root>
  28. <!--      XSL        -->
  29. <?xml version="1.0" encoding="utf-8"?>
  30. <xsl:stylesheet version="2.0"
  31.        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  32.        xmlns:bem="bem"
  33.        xmlns:m="bem:m">
  34.  
  35. <xsl:output method="html" version="4.0" encoding="UTF-8"/>
  36.  
  37.     <xsl:template match="*" mode="bem">
  38.         <div>
  39.             <xsl:apply-templates select="." mode="m"/>
  40.             <xsl:value-of select="normalize-space(text()[1])"/>
  41.             <xsl:apply-templates select="*" mode="bem"/>
  42.         </div>
  43.     </xsl:template>
  44.  
  45.     <xsl:template match="*" mode="m">
  46.         <xsl:copy-of select="@*[namespace-uri() = '' and name() != 'class']"/>
  47.         <xsl:choose>
  48.             <xsl:when test="starts-with(local-name(), 'b-')">
  49.                 <xsl:apply-templates select="." mode="b"/>
  50.             </xsl:when>
  51.             <xsl:otherwise>
  52.                 <xsl:apply-templates select="." mode="e"/>
  53.             </xsl:otherwise>
  54.         </xsl:choose>
  55.     </xsl:template>
  56.  
  57.     <xsl:template match="*" mode="b">
  58.         <xsl:attribute name="class">
  59.             <xsl:value-of select="local-name()"/>
  60.             <xsl:apply-templates select="@*[starts-with(name(), 'm:')]">
  61.                 <xsl:with-param name="name" select="local-name()"/>
  62.             </xsl:apply-templates>
  63.         </xsl:attribute>
  64.     </xsl:template>
  65.  
  66.     <xsl:template match="*" mode="e">
  67.         <xsl:param name="name">
  68.             <xsl:variable name="ancestors" select="ancestor::*[starts-with(local-name(), 'b-')]"/>
  69.             <xsl:value-of select="local-name($ancestors[position() = last()])"/>
  70.             <xsl:text>__</xsl:text>
  71.             <xsl:value-of select="local-name()"/>
  72.         </xsl:param>
  73.  
  74.         <xsl:attribute name="class">
  75.             <xsl:value-of select="$name"/>
  76.             <xsl:apply-templates select="@*[starts-with(name(), 'm:')]">
  77.                 <xsl:with-param name="name" select="$name"/>
  78.             </xsl:apply-templates>
  79.         </xsl:attribute>
  80.     </xsl:template>
  81.  
  82.     <xsl:template match="@*[starts-with(name(), 'm:')]">
  83.         <xsl:param name="name"/>
  84.         <xsl:text> </xsl:text>
  85.         <xsl:value-of select="$name"/>
  86.         <xsl:text>_</xsl:text>
  87.         <xsl:value-of select="local-name()"/>_<xsl:value-of select="."/>
  88.     </xsl:template>
  89.  
  90.     <xsl:template match="@*"/>
  91.  
  92.     <xsl:function name="bem:block">
  93.         <xsl:param name="block"/>
  94.         <xsl:apply-templates select="$block" mode="bem"/>
  95.     </xsl:function>
  96.  
  97.     <xsl:template match="/">
  98.         <bem>
  99.             <xsl:copy-of select="bem:block(*/*)"/>
  100.         </bem>
  101.     </xsl:template>
  102. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement