Xslt 如何剥离依赖于上下文的标记?
假设我有一个如下的源示例文档:Xslt 如何剥离依赖于上下文的标记?,xslt,Xslt,假设我有一个如下的源示例文档: <html> <b><i><u>TestBIU</u></i></b> <i><b><u>TestIBU</u></b></i> <i><u><b>TestIUB</b></u></i> <b><u>
<html>
<b><i><u>TestBIU</u></i></b>
<i><b><u>TestIBU</u></b></i>
<i><u><b>TestIUB</b></u></i>
<b><u><i>TestBUI</i></u></b>
<u><i><b>TestUIB</b></i></u>
<u><b><i>TestUBI</i></b></u>
<u>TestU</u>
<i>TestI</i>
<b>TestB</b>
<u><b>TestUB</b></u>
</html>
TestBIU
睾丸
睾丸
TestBUI
TestUIB
TestUBI
TestU
睾丸
测试B
TestUB
我需要生成以下内容的XSLT模板:
<html>
<b><i>TestBIU</i></b>
<i><b>TestIBU</b></i>
<i><b>TestIUB</b></i>
<b><i>TestBUI</i></b>
<i><b>TestUIB</b></i>
<b><i>TestUBI</i></b>
<u>TestU</u>
<i>TestI</i>
<b>TestB</b>
<b>TestUB</b>
</html>
TestBIU
睾丸
睾丸
TestBUI
TestUIB
TestUBI
TestU
睾丸
测试B
TestUB
因此,当下划线标记与斜体和/或粗体标记一起出现时,它应该删除下划线标记。当只加下划线时,它应保持不变。
有没有办法解决这个问题
这是我的尝试,但如果TestUIB和TestUBI失败:
<xsl:template match="/">
<html>
<xsl:apply-templates />
</html>
</xsl:template>
<xsl:template match="b/u">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="i/u">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="u/i">
<i><xsl:apply-templates /></i>
</xsl:template>
<xsl:template match="u/b">
<b><xsl:apply-templates /></b>
</xsl:template>
<xsl:template match="b | u | i">
<xsl:copy>
<xsl:apply-templates select="* | text()"/>
</xsl:copy>
</xsl:template>
我想
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="b//u | i//u | u[b] | u[i]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
够了。试试这个:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="u[ancestor::b | ancestor::i | descendant::b | descendant::i]">
<xsl:apply-templates />
</xsl:template>
</xsl:stylesheet>
它为您的示例生成以下输出:
<html>
<b><i>TestBIU</i></b>
<i><b>TestIBU</b></i>
<i><b>TestIUB</b></i>
<b><i>TestBUI</i></b>
<i><b>TestUIB</b></i>
<b><i>TestUBI</i></b>
<u>TestU</u>
<i>TestI</i>
<b>TestB</b>
<b>TestUB</b>
</html>
TestBIU
睾丸
睾丸
TestBUI
TestUIB
TestUBI
TestU
睾丸
测试B
TestUB
说明:在我的解决方案中,我使用的是逐节点和逐属性复制所有内容的方法。第二个模板截取所有在其祖先或后代中具有
或
的
HTML标记。出现这种情况时,我们不会复制标记,而只会调用将处理其子项的应用模板。甚至可以通过内容进行匹配吗?Lukasz和Martin提出的解决方案之间是否存在语义差异?两者似乎都有效。有一个区别,我使用u[b]|u[I]
来处理u
元素作为子元素包含b
或I
的情况,这就足以满足您提供的示例数据。卢卡斯进一步讨论了u[genderant::b | genderant::i]
,因为它还假设b
或i
可以作为后代出现(例如,…
),在这种情况下,您也希望将u
剥离。啊,我明白了。在这种情况下,卢卡斯的解决方案更适合我的需要。谢谢你们两位的回答,这很有帮助。我对两个答案都投了票。这也是version=“1.0”解决方案。