在特定标记的位置使用XSLT1.0拆分XML,并在整个结果中维护HTML标记
我需要通过在在特定标记的位置使用XSLT1.0拆分XML,并在整个结果中维护HTML标记,html,xml,xslt,Html,Xml,Xslt,我需要通过在标记处拆分来转换以下XML。然后,拆分两侧的内容应存储为参数,并在单独的div中输出 我遇到了几个问题: 1) 我似乎不能使用标记作为分隔符。我假设这是因为解析器在设置新参数之前剥离了标记?有解决办法吗?我确实可以控制节点中的XML输出,虽然我可以使用指定的字符串,但是标记有助于更可靠的标记维护 2) 虽然生成完整的HTML标记内容(我需要),但$shortDesc和$longDesc都返回裸文本内容。如何保留HTML标记 基本上,我需要在标记处拆分XML,然后将前后内容复制到单独的
标记处拆分来转换以下XML。然后,拆分两侧的内容应存储为参数,并在单独的div中输出
我遇到了几个问题:
1) 我似乎不能使用
标记作为分隔符。我假设这是因为解析器在设置新参数之前剥离了标记?有解决办法吗?我确实可以控制
节点中的XML输出,虽然我可以使用指定的字符串,但是
标记有助于更可靠的标记维护
2) 虽然
生成完整的HTML标记内容(我需要),但$shortDesc
和$longDesc
都返回裸文本内容。如何保留HTML标记
基本上,我需要在
标记处拆分XML,然后将前后内容复制到单独的div中,标记保持不变。
我看过一些帖子,其中的挑战与此非常接近,但每个帖子都有很大的不同,以至于我无法解决
XML:
<bodyText>
<p>Lorem Ipsum Short</p>
<hr/>
<p>Lorem Ipsum Long</p>
</bodyText>
腰短
腰长
*请注意,XML提要是由管理员生成的,所以这只是一个基本示例。实际馈送不可靠,只是应该发生拆分的位置将有一个
XSL:
<xsl:template name="ExpandContent_main">
<xsl:param name="desc" select="/bodyText"/>
<xsl:param name="separator" select="'<hr/>'"/>
<xsl:param name="shortDesc" select="substring-before($desc, $separator)"/>
<xsl:param name="longDesc" select="substring-after($desc, $separator)"/>
<div class="short-description">
<xsl:copy-of select="$shortDesc"/>
</div>
<div class="long-description">
<xsl:copy-of select="$longDesc"/>
</div>
</xsl:template>
预期产出:
<div class="short-description">
<p>Lorem Ipsum Short</p>
</div>
<div class="long-description">
<p>Lorem Ipsum Long</p>
</div>
腰短
腰长
您的错误是试图在节点上使用字符串函数。现在,我认为您不需要为此命名模板。请考虑下面的样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bodyText">
<div class="short-description">
<xsl:apply-templates select="p[not(preceding-sibling::hr)]"/>
</div>
<div class="long-description">
<xsl:apply-templates select="p[preceding-sibling::hr]"/>
</div>
</xsl:template>
</xsl:stylesheet>
当应用于以下测试输入时:
<content>
<bodyText>
<p>Lorem <i>Ipsum</i> Short</p>
<hr/>
<p>Lorem <b>Ipsum</b> Long</p>
</bodyText>
</content>
腰短
腰长
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<content>
<div class="short-description">
<p>Lorem <i>Ipsum</i> Short</p>
</div>
<div class="long-description">
<p>Lorem <b>Ipsum</b> Long</p>
</div>
</content>
腰短
腰长
您的错误是试图在节点上使用字符串函数。现在,我认为您不需要为此命名模板。请考虑下面的样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="bodyText">
<div class="short-description">
<xsl:apply-templates select="p[not(preceding-sibling::hr)]"/>
</div>
<div class="long-description">
<xsl:apply-templates select="p[preceding-sibling::hr]"/>
</div>
</xsl:template>
</xsl:stylesheet>
当应用于以下测试输入时:
<content>
<bodyText>
<p>Lorem <i>Ipsum</i> Short</p>
<hr/>
<p>Lorem <b>Ipsum</b> Long</p>
</bodyText>
</content>
腰短
腰长
结果将是:
<?xml version="1.0" encoding="UTF-8"?>
<content>
<div class="short-description">
<p>Lorem <i>Ipsum</i> Short</p>
</div>
<div class="long-description">
<p>Lorem <b>Ipsum</b> Long</p>
</div>
</content>
腰短
腰长
请发布您的预期输出。也指示XSLT 1或2.0感谢@ Migel.HOR257K,更新。在<代码> <代码>中是否总是有一个完全正确的代码< > HR/> < /代码>标签?是的,我们可以考虑这条规则。只有一个标签。为此绑定到XSLT 1.0。请发布您的预期输出。也指示XSLT 1或2.0感谢@ Migel.HOR257K,更新。在<代码> <代码>中是否总是有一个完全正确的代码< > HR/> < /代码>标签?是的,我们可以考虑这条规则。只有一个
标签。为此绑定到XSLT1.0。关于在节点上使用字符串函数的注释,请确定+1。非常感谢。Definite+1是关于在节点上使用字符串函数的注释。非常感谢。