我正在尝试创建一个xsl- trying表,以正确的顺序输出我的xml内容。下面是一个示例: XML:
...<p>This is<mark> a nested <b>text</b></mark></p>...XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<body>
<h2>
<xsl:value-of select="html/head/title"/>
</h2>
<div style="border:1px solid black;margin:30px;padding:30px;box-sizing:border-box;">
<xsl:for-each select="html/body/div[@class='toc']/table/tr/td/a">
<p><a>
<xsl:attribute name="href" namespace="uri">
<xsl:value-of select="current()/@href"/>
</xsl:attribute>
<xsl:value-of select="current()"/>
</a></p>
</xsl:for-each>
</div>
<xsl:for-each select="html/body/div[@class='chapter']">
<div style="border:1px solid black;margin:30px;padding:30px;box-sizing:border-box;">
<xsl:attribute name="id" namespace="uri"><xsl:value-of select ="current()/@id"/></xsl:attribute>
<p><xsl:value-of select ="current()/@id"/></p>
<xsl:call-template name="rec">
<xsl:with-param name="parents" select="current()"/>
</xsl:call-template>
</div>
</xsl:for-each>
</body>
</html>
</xsl:template>
<xsl:template name="rec">
<xsl:param name="parents"></xsl:param>
<xsl:for-each select="$parents/*">
<xsl:if test="name() = 'img'">
<img class="{@class}" src="{@src}" style="max-width:100%;"/>
</xsl:if>
<xsl:if test="name() != 'img'">
<xsl:element name="{local-name()}">
<xsl:if test="name() != 'figure'">
<xsl:value-of select ="current()"/>
</xsl:if>
<xsl:call-template name="rec">
<xsl:with-param name="parents" select="current()"/>
</xsl:call-template>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>这将输出以下内容:
This is a nested text
a nested text
text我想要得到的是:
<p>This is<mark> a nested <b>text</b></mark></p>我尝试只包含一个CSS样式表(这将摆脱这个特殊的问题),但这似乎不适用于图像(例如),它不会显示,但会发生在大多数文档中。XSL-Stylesheet应该可以处理多个文档(我编写了一个导出器,用于创建大致遵循相同语法的xml文件)。重要的部分应该只是<xsl:template name="rec">中的递归函数。
我们将非常感谢您的帮助。谢谢!
发布于 2021-08-17 15:07:52
基本的推送样式、结构和顺序保留处理通常依赖于身份转换模板以及需要转换的每个节点的自定义模板。
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="img">
<img class="{@class}" src="{@src}" style="max-width:100%;"/>
</xsl:template>错误输出中的重复文本是通过在递归命名模板中重复使用xsl:value-of而创建的。如果您将文本视为节点,并通过适当的模板处理任何复制,例如身份转换模板,则不会多次输出文本值。
https://stackoverflow.com/questions/68818371
复制相似问题