gpt4 book ai didi

html - 创建 HTML 标记的更好的 XSL 样式方法是什么?

转载 作者:可可西里 更新时间:2023-11-01 14:53:33 26 4
gpt4 key购买 nike

我有一个相当简单的 xsl 样式表,用于将定义我们的 html 的 xml 文档转换为 html 格式(请不要问为什么,这就是我们必须这样做的方式......)

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

<xsl:template match="HtmlElement">
<xsl:element name="{ElementType}">
<xsl:apply-templates select="Attributes"/>
<xsl:value-of select="Text"/>
<xsl:apply-templates select="HtmlElement"/>
</xsl:element>
</xsl:template>

<xsl:template match="Attributes">
<xsl:apply-templates select="Attribute"/>
</xsl:template>

<xsl:template match="Attribute">
<xsl:attribute name="{Name}">
<xsl:value-of select="Value"/>
</xsl:attribute>
</xsl:template>

当我遇到需要转换的这一小段 HTML 时,问题出现了:

<p>
Send instant ecards this season <br/> and all year with our ecards!
</p>

<br/>中间打破了转换的逻辑,只给我段落 block 的前半部分:Send instant ecards this season <br></br> .尝试转换的 XML 如下所示:

<HtmlElement>
<ElementType>p</ElementType>
<Text>Send instant ecards this season </Text>
<HtmlElement>
<ElementType>br</ElementType>
</HtmlElement>
<Text> and all year with our ecards!</Text>
</HtmlElement>

建议?

最佳答案

您可以简单地为 Text 元素添加一个新规则,然后匹配 HTMLElements 和 Texts:

<xsl:template match="HtmlElement">
<xsl:element name="{ElementName}">
<xsl:apply-templates select="Attributes"/>
<xsl:apply-templates select="HtmlElement|Text"/>
</xsl:element>
</xsl:template>

<xsl:template match="Text">
<xsl:value-of select="." />
</xsl:template>

关于html - 创建 HTML 标记的更好的 XSL 样式方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14165994/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com