2017-01-27 12 views
0

スタンドアロン属性width="<>"height="<>"style="width:<>; height:<>;に置き換える必要があります。XML属性をHTMLファイルのstyle = ""に移動

典型的な入力には、以下の

<img width="32pt" alt="PIC" class="graphics" height="32pt" src="images/about.png"/> 

このように結果が

<img style="width:32pt; height:32pt;" alt="PIC" class="graphics" src="images/about.png"/> 

問題は、私は前にXSLで働いたことがないということであるべきです。

は、私が知っている何を、限り、

この本はwidth属性

<xsl:template match="@width"> 

をキャッチすることができます

<xsl:template match="xh:img"> 

img要素をキャッチすることができますということであり、私は追加する方法を知っています属性または要素。 しかし、私はwidthheightの値をどのように格納し、両方を1行に書くのか分かりません。 ご協力いただければ幸いです。

あなたが使用することができ

答えて

1

<xsl:template match="img"> 
    <img style="width:{@width}; height:{@height};"> 
     <xsl:copy-of select="@*[not(name()='width' or name()='height')]"/> 
    </img> 
</xsl:template> 

これは、すべてのimgwidthheight属性、および子ノードの両方を持っていると仮定しています。ソースXMLでimg要素をネームスペース(質問には表示されません)に配置する場合は、テンプレートの一致パターンに適切な接頭辞を追加します。参考のため

、以下を参照してくださいhttps://www.w3.org/TR/xslt/#attribute-value-templates

+0

どうもありがとう!私は '' not() ''を最後に置いて修正しなければならなかったが、うまくいきました。 – marco

+0

良いキャッチ - 今すぐ修正されました。 –

0

は、あなた(または他の読者は)生産スタイルシートから取られたこのより一般的な(XSLT 2.0)溶液中で興味があるかもしれない:

<xsl:template name="style-attributes"> 
    <xsl:choose> 
     <xsl:when test="@style | @class"> 
     <xsl:copy-of select="@style | @class"/> 
     </xsl:when> 
     <xsl:when test="@*[f:is-style-attribute(.)]"> 
     <xsl:attribute name="style"> 
      <xsl:for-each select="@*[f:is-style-attribute(.)]"> 
      <xsl:if test="position() gt 1">; </xsl:if> 
      <xsl:apply-templates select="." mode="style-attribute"/> 
      </xsl:for-each> 
     </xsl:attribute> 
     </xsl:when> 
    </xsl:choose> 
    </xsl:template> 

    <xsl:function name="f:is-style-attribute" as="xs:boolean"> 
    <xsl:param name="att" as="attribute(*)"/> 
    <xsl:variable name="n" select="local-name($att)"/> 
    <xsl:sequence select="namespace-uri($att) = '' and $n = ('class', 'style', 'border', 'width', 'cellspacing', 'padding', 'cellpadding', 'align', 'valign')"/> 
    </xsl:function> 

    <xsl:function name="f:units" as="xs:string"> 
    <xsl:param name="value" as="xs:string"/> 
    <xsl:sequence select="if ($value castable as xs:integer) then concat($value, 'px') else $value"/> 
    </xsl:function> 

    <xsl:template match="@border" mode="style-attribute">border:<xsl:value-of select="f:units(.)"/> solid</xsl:template> 
    <xsl:template match="@width" mode="style-attribute">width:<xsl:value-of select="f:units(.)"/></xsl:template> 
    <xsl:template match="@align" mode="style-attribute">text-align:<xsl:value-of select="."/></xsl:template> 
    <xsl:template match="@valign" mode="style-attribute">vertical-align:<xsl:value-of select="."/></xsl:template> 
    <xsl:template match="@cellspacing" mode="style-attribute">border-spacing:<xsl:value-of select="."/></xsl:template> 
    <xsl:template match="@padding" mode="style-attribute">padding:<xsl:value-of select="f:units(.)"/></xsl:template> 
    <xsl:template match="@cellpadding" mode="style-attribute">padding:<xsl:value-of select="f:units(.)"/></xsl:template> 
    ... etc (as required) 
関連する問題