2016-08-29 4 views
4

私はHTMLベースの形式に親の最初の子の属性をXSLTの親に移動するにはどうすればよいですか?

をフラッシュテキスト形式に変換しようとしている、XSLTへの初心者だソースXMLで<LI></LI>ブロックし、すべての<LI>のブロックがあり、1つのまたは多数の<FONT>のノードが含まれています。インラインCSSの<FONT>のスタイルを<LI>に適用し、<FONT>ノード(最初のFONT子)を削除する必要があります。

(唯一の説明のための例 - 開始) から:

<LI> 
    <FONT FACE="Lato" SIZE="24" COLOR="#F7941D" LETTERSPACING="0" KERNING="0"> 
     <I>ertrr</I> 
     <FONT SIZE="12" COLOR="#4B4B4B">sdfsd</FONT> 
    </FONT> 
</LI> 

へ:

<li style="font-family:Lato; font-size:24px; color:#F7941D;"> 
    <I>ertrr</I> 
    <span style="font-size:12px; color:#4B4B4B;">sdfsd</span> 
</li> 

(唯一の説明のための例 - エンド)

XMLソース

<root> 
    <TEXTFORMAT LEADING="2"> 
     <LI> 
      <FONT FACE="Lato" SIZE="24" COLOR="#F7941D" LETTERSPACING="0" KERNING="0"> 
       <I>ertrr</I> 
       <FONT SIZE="12" COLOR="#4B4B4B"></FONT> 
      </FONT> 
     </LI> 
    </TEXTFORMAT> 
    <TEXTFORMAT LEADING="2"> 
     <LI> 
      <FONT FACE="Lato" SIZE="24" COLOR="#000000" LETTERSPACING="0" KERNING="0"> 
       <I><U>ert</U></I> 
       <FONT SIZE="12" COLOR="#4B4B4B"></FONT> 
      </FONT> 
     </LI> 
    </TEXTFORMAT> 
    <TEXTFORMAT LEADING="2"> 
     <LI> 
      <FONT FACE="System" SIZE="16" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0"> 
       <B>hgjgj</B> 
       <FONT FACE="Lato" SIZE="12"></FONT> 
      </FONT> 
     </LI> 
    </TEXTFORMAT> 
    <TEXTFORMAT LEADING="2"> 
     <LI> 
      <FONT FACE="System" SIZE="16" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0">ghjghj 
       <FONT FACE="Lato" SIZE="12"></FONT> 
      </FONT> 
     </LI> 
    </TEXTFORMAT> 
    <TEXTFORMAT LEADING="2"> 
     <LI> 
      <FONT FACE="Lato" SIZE="12" COLOR="#4B4B4B" LETTERSPACING="0" KERNING="0">@#dgsdg 
       <FONT FACE="Gabriola">sdfgdfg</FONT> dsfg df 
       <FONT SIZE="16">gdsfg</FONT>sd s 
       <FONT FACE="Lucida Console">d 
        <I>fg df</I> gs 
        <FONT FACE="Verdana">dg sdgfgsd</FONT> 
       </FONT> gdfg </FONT> 
     </LI> 
    </TEXTFORMAT> 
    <TEXTFORMAT LEADING="2"> 
     <LI> 
      <FONT FACE="Lato" SIZE="24" COLOR="#000000" LETTERSPACING="0" KERNING="0"> 
       <I><U>ert</U></I> 
       <FONT SIZE="12" COLOR="#4B4B4B">sdfsd</FONT> 
      </FONT> 
     </LI> 
    </TEXTFORMAT> 

</root> 

予想される出力

<div> 
     <li style="font-family:Lato; font-size:24px; color:#F7941D;"> 
      <I>ertrr</I><span style="font-size:12px; color:#4B4B4B;"></span> 
     </li> 
     <li style="font-family:Lato; font-size:24px; color:#000000;"> 
      <I><U>ert</U></I><span style="font-size:12px; color:#4B4B4B;"></span> 
     </li> 
     <li style="font-family:System; font-size:16px; color:#4B4B4B;"> 
      <B>hgjgj</B><span style="font-family:Lato; font-size:12px; "></span> 
     </li> 
     <li style="font-family:System; font-size:16px; color:#4B4B4B;"> 
      ghjghj 
      <span style="font-family:Lato; font-size:12px; "></span> 
     </li> 
     <li style="font-family:Lato; font-size:12px; color:#4B4B4B;"> 
      @#dgsdg 
      <span style="font-family:Gabriola; ">sdfgdfg</span> dsfg df 
      <span style="font-size:16px; ">gdsfg</span>sd s 
      <span style="font-family:Lucida Console; ">d 
         <I>fg df</I> gs 
         <span style="font-family:Verdana; ">dg sdgfgsd</span></span> gdfg 
     </li> 
     <li style="font-family:Lato; font-size:24px; color:#000000;"> 
      <I><U>ert</U></I><span style="font-size:12px; color:#4B4B4B;">sdfsd</span> 
     </li> 
    </div> 

マイコード:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes" method="html"/> 

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

    <xsl:template match="root"> 
     <div> 
      <xsl:apply-templates/> 
     </div> 
    </xsl:template> 

    <xsl:template match="FONT"> 
     <span> 
      <xsl:attribute name="style"> 
       <!-- collect attributes --> 
       <xsl:variable name="styles"> 
        <xsl:if test="@FACE"> 
         <xsl:value-of select="concat('font-family:', @FACE)"/> 
         <xsl:text>; </xsl:text> 
        </xsl:if> 
        <xsl:if test="@SIZE"> 
         <xsl:value-of select="concat('font-size:', @SIZE, 'px')"/> 
         <xsl:text>; </xsl:text> 
        </xsl:if> 
        <xsl:if test="@COLOR"> 
         <xsl:value-of select="concat('color:', @COLOR)"/> 
         <xsl:text>;</xsl:text> 
        </xsl:if> 
       </xsl:variable> 
       <xsl:value-of select="$styles"/> 
      </xsl:attribute> 
      <xsl:apply-templates/> 
     </span> 
    </xsl:template> 

    <!-- remove unwanted attributes --> 
    <xsl:template match="@LETTERSPACING|@KERNING"/> 

    <!-- Replace <LI> with <li> --> 
    <xsl:template match="LI"> 
     <li> 
      <xsl:attribute name="style"> 
       <!-- collect attributes --> 
       <xsl:variable name="styles"> 
        <xsl:if test="FONT/@FACE"> 
         <xsl:value-of select="concat('font-family:', FONT/@FACE)"/> 
         <xsl:text>; </xsl:text> 
        </xsl:if> 
        <xsl:if test="FONT/@SIZE"> 
         <xsl:value-of select="concat('font-size:', FONT/@SIZE, 'px')"/> 
         <xsl:text>; </xsl:text> 
        </xsl:if> 
        <xsl:if test="FONT/@COLOR"> 
         <xsl:value-of select="concat('color:', FONT/@COLOR)"/> 
         <xsl:text>;</xsl:text> 
        </xsl:if> 
       </xsl:variable> 
       <!-- delete trailing spaces --> 
       <xsl:value-of select="$styles"/> 
      </xsl:attribute> 
      <xsl:apply-templates/> 
     </li> 
    </xsl:template> 

    <!-- Remove TEXTFORMAT --> 
    <xsl:template match="TEXTFORMAT"> 
     <xsl:apply-templates/> 
    </xsl:template> 

</xsl:stylesheet> 

電流出力

<div> 
    <li style="font-family:Lato; font-size:24px; color:#F7941D;"> 
     <span style="font-family:Lato; font-size:24px; color:#F7941D;"><I>ertrr</I><span style="font-size:12px; color:#4B4B4B;"></span></span> 
    </li> 
    <li style="font-family:Lato; font-size:24px; color:#000000;"> 
     <span style="font-family:Lato; font-size:24px; color:#000000;"><I><U>ert</U></I><span style="font-size:12px; color:#4B4B4B;"></span></span> 
    </li> 
    <li style="font-family:System; font-size:16px; color:#4B4B4B;"> 
     <span style="font-family:System; font-size:16px; color:#4B4B4B;"><B>hgjgj</B><span style="font-family:Lato; font-size:12px; "></span></span> 
    </li> 
    <li style="font-family:System; font-size:16px; color:#4B4B4B;"> 
     <span style="font-family:System; font-size:16px; color:#4B4B4B;">ghjghj 
       <span style="font-family:Lato; font-size:12px; "></span></span> 
    </li> 
    <li style="font-family:Lato; font-size:12px; color:#4B4B4B;"> 
     <span style="font-family:Lato; font-size:12px; color:#4B4B4B;">@#dgsdg 
       <span style="font-family:Gabriola; ">sdfgdfg</span> dsfg df 
     <span style="font-size:16px; ">gdsfg</span>sd s 
     <span style="font-family:Lucida Console; ">d 
        <I>fg df</I> gs 
        <span style="font-family:Verdana; ">dg sdgfgsd</span></span> gdfg </span> 
    </li> 
    <li style="font-family:Lato; font-size:24px; color:#000000;"> 
     <span style="font-family:Lato; font-size:24px; color:#000000;"><I><U>ert</U></I><span style="font-size:12px; color:#4B4B4B;">sdfsd</span></span> 
    </li> 
</div> 
+0

質問は紛らわしいです。最初の例(「説明のみ」)は、** 2番目の例とは非常に異なります。 –

+0

@ michael.hor257k質問を更新しました – tymspy

答えて

1

属性を処理するには、名前付きテンプレートを作成する必要があります。以下は必要なスタイルシートです

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:strip-space elements="*"/> 
    <xsl:output indent="yes" omit-xml-declaration="yes"/> 

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

    <xsl:template match="LI"> 
     <li> 
      <xsl:attribute name="style"> 
       <xsl:call-template name="collect_attributes"> 
        <xsl:with-param name="FACE" select="*[1][name()='FONT']/@FACE"/> 
        <xsl:with-param name="SIZE" select="*[1][name()='FONT']/@SIZE"/> 
        <xsl:with-param name="COLOR" select="*[1][name()='FONT']/@COLOR"/> 
       </xsl:call-template> 
      </xsl:attribute> 
      <xsl:apply-templates/> 
     </li> 
    </xsl:template> 

    <xsl:template match="LI/FONT[1]"> 
     <xsl:apply-templates/> 
    </xsl:template> 

    <xsl:template match="FONT[not(parent::LI)]"> 
     <span> 
      <xsl:attribute name="style"> 
       <xsl:call-template name="collect_attributes"> 
        <xsl:with-param name="FACE" select="@FACE"/> 
        <xsl:with-param name="SIZE" select="@SIZE"/> 
        <xsl:with-param name="COLOR" select="@COLOR"/> 
       </xsl:call-template> 
      </xsl:attribute> 
     </span> 
    </xsl:template> 

    <!-- named template to process the attributes --> 
    <xsl:template name="collect_attributes"> 
     <xsl:param name="COLOR"/> 
     <xsl:param name="FACE"/> 
     <xsl:param name="SIZE"/> 

     <!-- collect attributes --> 
     <xsl:variable name="styles"> 
      <xsl:if test="string-length($FACE) &gt; 0"> 
       <xsl:value-of select="concat('font-family:', $FACE)"/> 
       <xsl:text>; </xsl:text> 
      </xsl:if> 
      <xsl:if test="string-length($SIZE) &gt; 0"> 
       <xsl:value-of select="concat('font-size:', $SIZE, 'px')"/> 
       <xsl:text>; </xsl:text> 
      </xsl:if> 
      <xsl:if test="string-length($COLOR) &gt; 0"> 
       <xsl:value-of select="concat('color:', $COLOR)"/> 
       <xsl:text>;</xsl:text> 
      </xsl:if> 
     </xsl:variable> 
     <!-- delete trailing spaces --> 
     <xsl:value-of select="normalize-space($styles)"/> 
    </xsl:template> 

    <xsl:template match="root"> 
     <div> 
      <xsl:apply-templates/> 
     </div> 
    </xsl:template> 

    <xsl:template match="TEXTFORMAT"> 
     <xsl:apply-templates/> 
    </xsl:template> 

</xsl:stylesheet> 
1

これはあなたの "唯一の説明のための例" を処理します:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="LI"> 
    <li> 
     <xsl:apply-templates select="FONT[1]" mode="style"/> 
     <xsl:apply-templates select="FONT[position() > 1]"/> 
    </li> 
</xsl:template> 

<xsl:template match="FONT"> 
    <span> 
     <xsl:attribute name="style"> 
      <xsl:apply-templates select="@*" mode="style"/> 
     </xsl:attribute> 
     <xsl:apply-templates/> 
    </span> 
</xsl:template> 

<xsl:template match="FONT" mode="style"> 
    <xsl:attribute name="style"> 
     <xsl:apply-templates select="@*" mode="style"/> 
    </xsl:attribute> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="@FACE" mode="style"> 
    <xsl:text>font-family:</xsl:text> 
    <xsl:value-of select="."/> 
    <xsl:text>; </xsl:text> 
</xsl:template> 

<xsl:template match="@SIZE" mode="style"> 
    <xsl:text>font-size:</xsl:text> 
    <xsl:value-of select="."/> 
    <xsl:text>; </xsl:text> 
</xsl:template> 

<xsl:template match="@COLOR" mode="style"> 
    <xsl:text>color:</xsl:text> 
    <xsl:value-of select="."/> 
    <xsl:text>; </xsl:text> 
</xsl:template> 

<xsl:template match="@*" mode="style"/> 

</xsl:stylesheet> 
+0

私はソースで他のいくつかの配慮があります、私は最初の子の属性を親に移動することについて説明しようとしていました。私は今質問を更新しました。ありがとう – tymspy

+0

私は私の答えを変更しました。次回は、回答した後に徹底的に質問を変更しないでください。 –

+0

本当に申し訳ありません、ありがとうございます – tymspy

関連する問題