- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我是 XSLT 的初学者,我正在尝试将 flash 文本格式转换为基于 HTML 的格式
有<LI></LI>
源 xml 中的 block 和所有 <LI>
block 包含 1 个或多个 <FONT>
节点。我需要应用 <FONT>
的样式在 <LI>
的内联 css 中并删除 <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>
最佳答案
您应该创建一个命名模板来处理属性。以下是您需要的样式表
<?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>
关于html - 在 XSLT 中如何将父项的第一个子项中的属性移动到父项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39205509/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!