Xslt 以适当的格式显示给定xml的节点值,如标记所述
输入文件格式Xslt 以适当的格式显示给定xml的节点值,如标记所述,xslt,Xslt,输入文件格式 <!DOCTYPE html> <html> <body> <p>I am normal</p> <p style="color:red;">I am<b> r<i>e</i>d</b></p> <p style="color:blue;">I am <b>blue<
<!DOCTYPE html>
<html>
<body>
<p>I am normal</p>
<p style="color:red;">I am<b> r<i>e</i>d</b></p>
<p style="color:blue;">I am <b>blue</b></p>
<p><b>I am <i>big</i></b></p>
</body>
</html>
从博文作者评论中获取的预期输出
<sp:html xmlns:"urn:unknown">
<sp:body>
<sp:p>I am normal</sp:p>
<sp:p>I am <sp:b emphasis="true">r <sp:i emphasis="true">
<sp:color textforecolor="red">e</sp:color>
</sp:i>
</sp:b>d </sp:p>
<sp:p>I am <sp:b emphasis="true">
<sp:color textforecolor="blue">blue</sp:color>
</sp:b>
</sp:p>
<sp:p>
<sp:b>I am<sp:span> </sp:span><sp:i>big</sp:i></sp:b>
</sp:p>
</sp:body>
</sp:html>
作者想知道如何将输入转换为给定的输出。我已编辑了您的问题,并将预期的输出添加到其中 此样式表与您在注释中发布的预期输出非常接近
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sp="urn:whatever"
exclude-result-prefixes="xs"
version="2.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="b">
<sp:b emphasis="true">
<xsl:apply-templates/>
</sp:b>
</xsl:template>
<xsl:template match="i">
<sp:i emphasis="true">
<xsl:apply-templates/>
</sp:i>
</xsl:template>
<xsl:template match="*">
<xsl:element name="sp:{local-name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但是,要达到预期的输出,还存在一些问题:
我添加了一个随机名称空间xmlns:sp=urn:whatever,因为您没有提供
颜色的输入和输出之间存在结构性差异。当输入中的颜色处于段落级别时,p style=color:red;,它在输出中仅应用于段落的一部分。我在这里看不到如何应用颜色的规则。
像这样的标记在输入中是相同的,但在输出和输出中有不同的结果。同一输入不能有不同的结果。必须有明确的转换规则。
如果您有明确的转换规则,您应该能够根据需要完成样式表。这个问题是什么?请通读并重新表述你的问题,提供所有细节 我是正常的
我是红色的我是蓝色的
我是大的这是html代码格式我想把它转换成XML你的问题一点也不清楚。非常感谢@burkiYou,不客气。我认为您无法成功编辑您的问题,因为HTML代码没有正确缩进。下次将代码复制/粘贴到文本编辑器中时,请修复缩进并将其复制/粘贴回问题中。这样,您应该能够消除验证错误,即您没有缩进代码。Ohk@burki sir。你的回答对我很有帮助。