XSLTに砂糖を追加する

通常のXSLTに小さな構文糖を追加してXSLTを変換する単純なテンプレート。 説明の代わりに、すべての新しいデザインが使用される小さな例を示します。



<px:each="item" x:if-attr="position() mod 2" x:attr-class="'odd'" xmlns:x="xslt-sugar"> <span class="date" x:value="date"/> <xsl:value-of select="title"/> <x:block x:if="description" x:value="concat(' ', description)" x:doe="yes"/> </p>
      
      





このコードを変換すると、通常のXSLTが取得されます。

 <xsl:for-each select="item"> <p> <xsl:if test="position() mod 2"> <xsl:attribute name="class"><xsl:value-of select="'odd'"/></xsl:attribute> </xsl:if> <span class="date"><xsl:value-of select="date"/></span> <xsl:value-of select="title"/> <xsl:if test="description"> <xsl:value-of select="concat(' ', description)" disable-output-escaping="yes"/> </xsl:if> </p> </xsl:for-each>
      
      



パラメーターx:attr-name="val"



<xsl:value-of select="val"/>



を介して出力されるため、例のように文字列パラメーターを引用符で囲むことに注意してください。



実際にはxslsugar.xsltテンプレート自体
 <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="xslt-sugar" version="1.0"> <xsl:output method="xml" indent="yes" encoding="utf-8"/> <xsl:template match="*|@*|text()|processing-instruction()|comment()"> <xsl:copy> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:copy> </xsl:template> <xsl:template match="x:*|@x:*"/> <xsl:template match="x:block"> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:template> <xsl:template match="@x:value"> <xsl:element name="xsl:value-of" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="select"><xsl:value-of select="."/></xsl:attribute> <xsl:if test="../@x:doe"><xsl:attribute name="disable-output-escaping"><xsl:value-of select="../@x:doe"/></xsl:attribute></xsl:if> </xsl:element> </xsl:template> <xsl:template match="*[@x:if]"> <xsl:element name="xsl:if" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="test"><xsl:value-of select="@x:if"/></xsl:attribute> <xsl:apply-templates select="." mode="element"/> </xsl:element> </xsl:template> <xsl:template match="*[@x:each]"> <xsl:element name="xsl:for-each" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="select"><xsl:value-of select="@x:each"/></xsl:attribute> <xsl:apply-templates select="." mode="element"/> </xsl:element> </xsl:template> <xsl:template match="@x:if-attr"> <xsl:element name="xsl:if" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="test"><xsl:value-of select="."/></xsl:attribute> <xsl:for-each select="../@x:*[starts-with(local-name(), 'attr-')]"> <xsl:element name="xsl:attribute" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="name"><xsl:value-of select="substring(local-name(), 6)"/></xsl:attribute> <xsl:element name="xsl:value-of" namespace="http://www.w3.org/1999/XSL/Transform"> <xsl:attribute name="select"><xsl:value-of select="."/></xsl:attribute> </xsl:element> </xsl:element> </xsl:for-each> </xsl:element> </xsl:template> <xsl:template match="*" mode="element"> <xsl:copy> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:copy> </xsl:template> <xsl:template match="x:block" mode="element"> <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/> </xsl:template> </xsl:stylesheet>
      
      







それをどのように使用し、どのように機会を拡大するかはあなた自身で決定します。ここにアイデアがあります。



PS:Sugar XSLTを変換するスクリプト:
 #!/bin/sh cp $1 $1.bak xsltproc -o $1 xslsugar.xslt $1
      
      



Windowsでは、これは次の方法で実行できます。
 msxsl.exe -o template.xslt template.xslt xslsugar.xslt
      
      



(試しませんでした)



PPS:正しくないが便利な既存のxmlns:xslを使用する場合は、xnslsugar.xsltのxmlns:x="xslt-sugar"



名前空間をxmlns:x="http://www.w3.org/1999/XSL/Transform"



に置き換えてxmlns:x="http://www.w3.org/1999/XSL/Transform"



および空のテンプレートからx:*



を削除します: <xsl:template match="@x:*"/>



、その結果、追加のネームスペースなしで記述できます: <p xsl:each="item">







次のバージョン、およびさらなる開発、 code.google.com / p / xslt-sugarを参照



All Articles