智能字符串中断:通过XSLT输出漂亮的打印makefile
我需要通过XSLT(xsltproc)漂亮地打印make的一些输出,重点是gcc调用的易读性(因为它们是上述输出中最常见和最重要的行) 情景:我的源XML文件中有很多长的智能字符串中断:通过XSLT输出漂亮的打印makefile,xslt,string,Xslt,String,我需要通过XSLT(xsltproc)漂亮地打印make的一些输出,重点是gcc调用的易读性(因为它们是上述输出中最常见和最重要的行) 情景:我的源XML文件中有很多长的gcc-Wall…节点,每个节点输出一行原始makefile。我的XSLT工作表的输出将是HTML,将节点的内容逐行放入 <message priority="info"><![CDATA[/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SH
gcc-Wall…
节点,每个
节点输出一行原始makefile。我的XSLT工作表的输出将是HTML,将
节点的内容逐行放入
<message priority="info"><![CDATA[/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. main.cpp -o main.moc]]></message>
<message priority="info"><![CDATA[/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3/AspectC++/bin/linux-release/ag++ -p /tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3/1297456240104_09fad65d9a05790369dd919025284109_20110211213211/qt-examples --Xcompiler -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp]]></message>
<message priority="info"><![CDATA[/tmp/main.cpp_agxx_c5k6Om: In destructor 'AC::ResultBuffer<T>::~ResultBuffer() [with T = QRectF]':]]></message>
<message priority="info"><![CDATA[/tmp/main.cpp_agxx_c5k6Om:216:33: instantiated from here]]></message>
<message priority="info"><![CDATA[/tmp/main.cpp_agxx_c5k6Om:26:24: warning: dereferencing type-punned pointer will break strict-aliasing rules]]></message>
<xsl:template name="break-string">
<xsl:param name="string" />
<xsl:choose>
<xsl:when test="string-length($string) <= $break-at">
<xsl:value-of select="$string"/><xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="contains($string, ' -')">
<xsl:variable name="out" select="substring-before($string, ' -')"/>
<xsl:choose>
<xsl:when test="string-length($out) <= $break-at">
<xsl:value-of select="$out"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="break-string">
<xsl:with-param name="string"
select="$out" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
<xsl:text>
</xsl:text>
<span class="indent"><xsl:text><![CDATA[ ]]></xsl:text></span><xsl:text>-</xsl:text>
<xsl:call-template name="break-string">
<xsl:with-param name="string"
select="substring-after($string, ' -')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($string, 1, $break-at)" /><xsl:text>
</xsl:text>
<span class="indent"><xsl:text><![CDATA[ ]]></xsl:text></span>
<xsl:call-template name="break-string">
<xsl:with-param name="string"
select="substring($string, $break-at + 1)" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
这是调用XSLT代码,在每一行输出上调用break string
模板:
断开这些字符串最优雅的方法是什么?您可以使用FXSL中现有的str拆分为行的模板:
/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB
-DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I.
-I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui
-I/usr/include/qt4 -I. main.cpp -o
添加我自己的无扩展:
<t><pre>/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB
-DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr
/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4
-I. main.cpp -o main.moc</pre><pre>/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/AspectC++/bin/linux-release/ag++ -p /tmp/Akut/work
/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/1297456240104_09fad65d9a05790369dd919025284109_20110211213211
/qt-examples --Xcompiler -c -pipe -O2 -Wall -W -D_REENTRANT
-DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr
/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I
/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp</pre><pre>/tmp/main.cpp_agxx_c5k6Om: In destructor 'AC::ResultBuffer<T>:
:~ResultBuffer() [with T = QRectF]'</pre><pre>/tmp/main.cpp_agxx_c5k6Om:216:33: instantiated from here</pre><pre>/tmp/main.cpp_agxx_c5k6Om:26:24: warning: dereferencing type
-punned pointer will break strict-aliasing rules</pre></t>
通过此输入:
/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/AspectC++/bin/linux-release/ag++ -p /tmp/Akut/work
/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/1297456240104_09fad65d9a05790369dd919025284109_20110211213211
/qt-examples --Xcompiler -c -pipe -O2 -Wall -W -D_REENTRANT
-DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr
/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I
/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp
/tmp/main.cpp_agxx_c5k6Om: In destructor 'AC::ResultBuffer<T>:
:~ResultBuffer() [with T = QRectF]'
输出:
/usr/bin/moc-DQT\u NO\u DEBUG-DQT\u GUI\u LIB-DQT\u CORE\u LIB
-DQT_SHARED-I/usr/share/qt4/mkspecs/linux-g++-I.-I/usr
/include/qt4/QtCore-I/usr/include/qt4/QtGui-I/usr/include/qt4
-I.main.cpp-o main.moc/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/AspectC++/bin/linux-release/ag++-p/tmp/Akut/work
/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/1297456240104_09FAD65D9A057900369DD919025284109_20110211213211
/qt示例——Xcompiler-c-pipe-O2-Wall-W-D_可重入
-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr
/share/qt4/mkspecs/linux-g++-I.-I/usr/include/qt4/QtCore-I
/usr/include/qt4/QtGui-I/usr/include/qt4-I.-o main.o main.cpp/tmp/main.cpp_agxx_c5k6Om:在析构函数'AC::ResultBufferT:
:~ResultBuffer()[with T=QRectF]'/tmp/main.cpp_agxx_c5k6Om:216:33:从此处实例化/tmp/main.cpp_agxx_c5k6Om:26:24:警告:取消引用类型
-双关指针将打破严格的别名规则
呈现:
/usr/bin/moc-DQT\u NO\u DEBUG-DQT\u GUI\u LIB-DQT\u CORE\u LIB
-DQT_SHARED-I/usr/share/qt4/mkspecs/linux-g++-I.-I/usr
/include/qt4/QtCore-I/usr/include/qt4/QtGui-I/usr/include/qt4
-I.main.cpp-o main.moc/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/AspectC++/bin/linux-release/ag++-p/tmp/Akut/work
/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/1297456240104_09FAD65D9A057900369DD919025284109_20110211213211
/qt示例——Xcompiler-c-pipe-O2-Wall-W-D_可重入
-DQT_NO_DEBUG-DQT_GUI_LIB-DQT_CORE_LIB-DQT_SHARED-I/usr
/share/qt4/mkspecs/linux-g++-I.-I/usr/include/qt4/QtCore-I
/usr/include/qt4/QtGui-I/usr/include/qt4-I.-o main.o main.cpp/tmp/main.cpp_agxx_c5k6Om:在析构函数'AC::ResultBuffer:
:~ResultBuffer()[with T=QRectF]'/tmp/main.cpp_agxx_c5k6Om:216:33:从此处实例化/tmp/main.cpp_agxx_c5k6Om:26:24:警告:取消引用类型
-双关指针将打破严格的别名规则
注意:基本的是多分隔符标记“尾随”分隔符(因为看起来您希望将分隔符与下一项一起保留)。我没有花太多时间,但打破逻辑可能会进一步简化。现在有三种情况:项目适合当前行,项目不适合整行(打断项目),项目不适合当前行但适合新行(新行,不打断项目)。好问题,+1。有关完整的解决方案,请参见我的答案。:)+1个有趣的问题。除了@Dimitre非常好的答案外,请参见我的不带扩展函数。看起来这就是答案。。。我现在无法测试它。你得等一下绿色的复选标记:)@maligree:没问题,欢迎你。如果你需要额外的帮助,请问任何问题。@Dimitre:好的,我终于有时间尝试了。在模板的第一次调用中,xsltproc会退出,并出现XPath错误:无效类型
。我怀疑罪魁祸首是select=“ext:node set($vrtfParams)”
,尽管我对复制和粘贴错误仍有点模糊(我已经将一些
编辑为
)。我仍然不确定是否有与您相同版本的dvc-str-foldl.xsl…@maligree:如果您的XSLT 1.0处理器没有实现EXSLTxxx:node-set()
扩展函数,FXSL还有一个MSXML和Xalan版本,您可以从sourceforge下载作为压缩存档。拥有和使用FXSL的强大之处在于,它有一些非常有用的现成模板/函数,例如str拆分为行
和str foldl
,在这种情况下,您可以“直接使用”而无需思考。这在很大程度上减少了开发时间。@maligree:至于转义
字符,根本没有必要,我不建议这样做,因为这会使代码变得非常不可读。@Alejandro+努力1:)如你所知,FXSL的用户节省了大量的努力,并且没有机会犯下无休止的重复错误,如果他们必须自己编写FXSL提供的内容,就会出现这种情况。@Dimitre:谢谢!没有太多的努力。。。我想我已经在一周内的三个答案中使用了多分隔符标记器。但请注意,Requestions将打断的单词作为最后的资源。我知道这将是一个优势
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="pSeparators" select="'
	 -/.:;='"/>
<xsl:param name="pMaxLength" select="64"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="message">
<pre>
<xsl:call-template name="tokenize"/>
</pre>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="pString" select="string()"/>
<xsl:param name="pMask"
select="translate(.,translate(.,$pSeparators,''),'')"/>
<xsl:param name="pLength" select="0"/>
<xsl:param name="pTail"/>
<xsl:if test="$pString">
<xsl:variable name="vSeparator"
select="substring($pMask,1,1)"/>
<xsl:variable name="vString"
select="concat(
$pTail,
substring-before(
$pString,
$vSeparator
),
substring(
$pString,
1 div not($vSeparator)
)
)"/>
<xsl:variable name="vLength"
select="string-length($vString)"/>
<xsl:variable name="vMaxLength"
select="$pMaxLength - $pLength"/>
<xsl:choose>
<xsl:when test="$vMaxLength >= $vLength">
<xsl:value-of select="$vString"/>
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="substring(
substring-after($pString,$vSeparator),
1 div boolean($vSeparator))"/>
<xsl:with-param name="pMask"
select="substring($pMask,2)"/>
<xsl:with-param name="pLength"
select="$pLength + $vLength"/>
<xsl:with-param name="pTail" select="$vSeparator"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="string-length(
normalize-space($vString)
) > $pMaxLength">
<xsl:value-of
select="concat(
substring(
$vString, 1, $vMaxLength
),
'
'
)"/>
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="substring(
$pString,
$vMaxLength - string-length($pTail) +1
)"/>
<xsl:with-param name="pMask" select="$pMask"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('
',
normalize-space($vString))"/>
<xsl:call-template name="tokenize">
<xsl:with-param name="pString"
select="substring(
substring-after($pString,$vSeparator),
1 div boolean($vSeparator))"/>
<xsl:with-param name="pMask"
select="substring($pMask,2)"/>
<xsl:with-param name="pLength" select="$vLength"/>
<xsl:with-param name="pTail" select="$vSeparator"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
<t>
<message priority="info"><![CDATA[/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. main.cpp -o main.moc]]></message>
<message priority="info"><![CDATA[/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3/AspectC++/bin/linux-release/ag++ -p /tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3/1297456240104_09fad65d9a05790369dd919025284109_20110211213211/qt-examples --Xcompiler -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp]]></message>
<message priority="info"><![CDATA[/tmp/main.cpp_agxx_c5k6Om: In destructor 'AC::ResultBuffer<T>::~ResultBuffer() [with T = QRectF]':]]></message>
<message priority="info"><![CDATA[/tmp/main.cpp_agxx_c5k6Om:216:33: instantiated from here]]></message>
<message priority="info"><![CDATA[/tmp/main.cpp_agxx_c5k6Om:26:24: warning: dereferencing type-punned pointer will break strict-aliasing rules]]></message>
</t>
<t><pre>/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB
-DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr
/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4
-I. main.cpp -o main.moc</pre><pre>/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/AspectC++/bin/linux-release/ag++ -p /tmp/Akut/work
/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/1297456240104_09fad65d9a05790369dd919025284109_20110211213211
/qt-examples --Xcompiler -c -pipe -O2 -Wall -W -D_REENTRANT
-DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr
/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I
/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp</pre><pre>/tmp/main.cpp_agxx_c5k6Om: In destructor 'AC::ResultBuffer<T>:
:~ResultBuffer() [with T = QRectF]'</pre><pre>/tmp/main.cpp_agxx_c5k6Om:216:33: instantiated from here</pre><pre>/tmp/main.cpp_agxx_c5k6Om:26:24: warning: dereferencing type
-punned pointer will break strict-aliasing rules</pre></t>
/usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB
-DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr
/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4
-I. main.cpp -o main.moc
/tmp/Akut/work/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/AspectC++/bin/linux-release/ag++ -p /tmp/Akut/work
/1296322206337_e01c972b8fe9b866aded56ff5dde35c3
/1297456240104_09fad65d9a05790369dd919025284109_20110211213211
/qt-examples --Xcompiler -c -pipe -O2 -Wall -W -D_REENTRANT
-DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr
/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I
/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -o main.o main.cpp
/tmp/main.cpp_agxx_c5k6Om: In destructor 'AC::ResultBuffer<T>:
:~ResultBuffer() [with T = QRectF]'
/tmp/main.cpp_agxx_c5k6Om:216:33: instantiated from here
/tmp/main.cpp_agxx_c5k6Om:26:24: warning: dereferencing type
-punned pointer will break strict-aliasing rules