如何将父项的第一个子项中的属性移动到XSLT中的父项? [英] How do you move an attribute in the first child of a parent to the parent in XSLT?
问题描述
我是XSLT的初学者,我试图将Flash文本格式转换为基于HTML的格式。
有 b来自: 收件人: (仅用于解释的示例 - 结束) XML Source 预期产量 我的代码: 目前产量 您应该创建一个命名模板来处理属性。以下是您需要的样式表 I'm a beginner to XSLT, I'm trying to convert flash Text Format into HTML based format There are ( Example for explanation only - start )
From: To: ( Example for explanation only - end ) XML Source Expected Output My code: Current Output
You should create a named template to process the attributes. The following is the stylesheet that you need
这篇关于如何将父项的第一个子项中的属性移动到XSLT中的父项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!< LI> ;< / code>块< / code>块< / code>块包含1个或多个
< FONT>
节点。我需要将内嵌css中的< FONT>
样式应用于< LI>
,并移除< FONT>
节点(第一个FONT子节点)。
< LI>
< FONT FACE =LatoSIZE =24COLOR =#F7941DLETTERSPACING =0KERNING =0>
< I> ertrr< / I>
< FONT SIZE =12COLOR =#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>
< root>
< TEXTFORMAT LEADING =2>
< LI>
< FONT FACE =LatoSIZE =24COLOR =#F7941DLETTERSPACING =0KERNING =0>
< I> ertrr< / I>
< FONT SIZE =12COLOR =#4B4B4B>< / FONT>
< / FONT>
< / LI>
< / TEXTFORMAT>
< TEXTFORMAT LEADING =2>
< LI>
< FONT FACE =LatoSIZE =24COLOR =#000000LETTERSPACING =0KERNING =0>
< I>< U> ert< / U>< / I>
< FONT SIZE =12COLOR =#4B4B4B>< / FONT>
< / FONT>
< / LI>
< / TEXTFORMAT>
< TEXTFORMAT LEADING =2>
< LI>
< FONT FACE =SystemSIZE =16COLOR =#4B4B4BLETTERSPACING =0KERNING =0>
< B> hgjgj< / B>
< FONT FACE =LatoSIZE =12>< / FONT>
< / FONT>
< / LI>
< / TEXTFORMAT>
< TEXTFORMAT LEADING =2>
< LI>
ghjghj
< / FONT>
< / LI>
< / TEXTFORMAT>
< TEXTFORMAT LEADING =2>
< LI>
< FONT FACE =LatoSIZE =12COLOR =#4B4B4BLETTERSPACING =0KERNING =0>##dgsdg
< 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 =LatoSIZE =24COLOR =#000000LETTERSPACING =0KERNING =0>
< I>< U> ert< / U>< / I>
< FONT SIZE =12COLOR =#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.0encoding =UTF-8?>
< xsl:stylesheet xmlns:xsl =http://www.w3.org/1999/XSL/Transform
version =1.0>
< xsl:strip-space elements =*/>
< xsl:output indent =yesmethod =html/>
<! - 身份模板 - >
< 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>
<! - - 收集属性 - >
< xsl:variable name =styles>
< xsl:if test =@ FACE>
< / xsl:if>
< xsl:if test =@ SIZE>
< / xsl:if>
< xsl:if test =@ COLOR>
< / xsl:if>
< / xsl:variable>
< xsl:value-of select =$ styles/>
< / xsl:attribute>
< xsl:apply-templates />
< / span>
< / xsl:template>
<! - 删除不需要的属性 - >
<! - 替换< LI>与< li> - >
< xsl:template match =LI>
< li>
< xsl:attribute name =style>
<! - - 收集属性 - >
< xsl:variable name =styles>
< xsl:if test =FONT / @ FACE>
< / xsl:if>
< xsl:if test =FONT / @ SIZE>
< / xsl:if>
< xsl:if test =FONT / @ COLOR>
< / xsl:if>
< / xsl:variable>
<! - 删除尾部空格 - >
< xsl:value-of select =$ styles/>
< / xsl:attribute>
< xsl:apply-templates />
< / li>
< / xsl:template>
<! - 删除TEXTFORMAT - >
< xsl:template match =TEXTFORMAT>
< xsl:apply-templates />
< / xsl:template>
< / xsl:stylesheet>
< DIV>
< li style =font-family:Lato; font-size:24px; color:#F7941D;>
< / li>
< li style =font-family:Lato; font-size:24px; color:#000000;>
< / li>
< li style =font-family:System; font-size:16px; color:#4B4B4B;>
< / li>
< li style =font-family:System; font-size:16px; color:#4B4B4B;>
< 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:Gabriola; > sdfgdfg< /跨度> 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;>
< / li>
< / div>
<?xml version =1.0encoding =utf-8?> ;
< xsl:stylesheet version =1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:strip-space elements =*/>
< xsl:output indent =yesomit-xml-declaration =yes/>
<! - 身份模板 - >
< 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: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 =COLORselect =@ COLOR/>
< / xsl:call-template>
< / xsl:attribute>
< / span>
< / xsl:template>
<! - 命名模板处理属性 - >
< xsl:template name =collect_attributes>
< xsl:param name =COLOR/>
< xsl:param name =FACE/>
< xsl:param name =SIZE/>
<! - - 收集属性 - >
< xsl:variable name =styles>
< xsl:if test =string-length($ FACE)& gt; 0>
< / xsl:if>
< xsl:if test =string-length($ SIZE)& gt; 0>
< xsl:value-of select =concat('font-size:',$ SIZE,'px')/>
< xsl:text>; < / XSL:文本>
< / 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>
<! - 删除尾部空格 - >
< 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>
<LI></LI>
blocks in the source xml and all <LI>
blocks contains 1 or many <FONT>
nodes. I need to apply the styles of the <FONT>
in inline css to the <LI>
and remove the <FONT>
node (first FONT child).<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>
<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>
<?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) > 0">
<xsl:value-of select="concat('font-family:', $FACE)"/>
<xsl:text>; </xsl:text>
</xsl:if>
<xsl:if test="string-length($SIZE) > 0">
<xsl:value-of select="concat('font-size:', $SIZE, 'px')"/>
<xsl:text>; </xsl:text>
</xsl:if>
<xsl:if test="string-length($COLOR) > 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>