开发者

Match multiple attribute values in html for ant buildscript

开发者 https://www.devze.com 2023-03-22 19:03 出处:网络
I want to match mulitple values of a attribute for replacing. for example <div class=\"div h1 full-width\"></div>

I want to match mulitple values of a attribute for replacing. for example

<div class="div h1 full-width"></div>

Should produces div, h1 and full-width as seperate matches. I want to do this to prefix the classes. So instead of div h1 full-width it should be pre-div pre-h1 pre-full-width

The regex I have sofar is

(?<=class=["'])(\b-?[_a-zA-Z]+[_a-zA-Z0-9-]*\b)+

This matches only the first class. This is offcourse because that is the only thing this pattern should match :( I tried to make the lookbehind take more then just class=" but I just end up with it taking everying and leaving nothing to replace. I want to make a pattern that matches any value individually between the quotes of the class attribute.

I want to do this for an Ant buildscript that processes all files and replaces the class="value1 value2 value3" with a set prefix. Ive done this with little 开发者_运维知识库trouble for replacing the classes in css files but ye html seems to be alot trickier.

It is a Ant buildscript. Java regexp package is used to process the pattern. The ant tag used is: replaceregexp

The ant implemtentation of above pattern is:

<target name="prefix-class" depends="">
  <replaceregexp flags="g">
    <regexp  pattern="(?&lt;=class=['&quot;])(\b-?[_a-zA-Z]+[_a-zA-Z0-9-]*\b)+"/>
    <substitution expression=".${prefix}\1"/>
    <fileset dir="${dest}"/>
   </replaceregexp>
</target>    


I don't think that you can find n (or in your case 3) different class entries and substitude them in one simple regexp. If you need to do this in ant i think you have to write your own ant task. A better way would be xslt, are you familiar with xslt?


Gave up on Ants ReplaceRegExp and sorted my problem with XSLT to transform xhtml to xhtml.

Following code adds a prefix to all values of a elements class attribute. the xhtml source document must be properly formatted to be parsed.

<xsl:stylesheet version="2.0"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xhtml xsl xs">

  <xsl:output method="xml" version="1.0" encoding="UTF-8" 
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd" 
    indent="yes" omit-xml-declaration="yes"/>

  <xsl:param name="prefix" select="'oo-'"/>

  <xsl:template match="/">
    <xsl:apply-templates select="./@*|./node()" />
  </xsl:template>

  <!--remove these atts from output, default xhtml values from dtd -->
  <xsl:template match="xhtml:a/@shape"/>
  <xsl:template match="@rowspan"/>
  <xsl:template match="@colspan"/>

  <xsl:template match="@class">
    <xsl:variable name="replace_regex">
      <xsl:value-of select="$prefix"/>
      <xsl:text>$1</xsl:text>
    </xsl:variable>
    <xsl:attribute name="class">
      <xsl:value-of select="fn:replace( . , '(\w+)' , $replace_regex )"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消