Xml XSLT删除子节点并保留带有标点符号的空白
我有Xml XSLT删除子节点并保留带有标点符号的空白,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我有格式的XML文件,其中包括一些未标记的内容,如空格和标点符号: <ref> <mixed-citation publication-type="book"> <collab>Collab</collab>. <source>Source</source>. <publisher-loc>Location</publisher-loc>: <publ
格式的XML文件,其中包括一些未标记的内容,如空格和标点符号:
<ref>
<mixed-citation publication-type="book">
<collab>Collab</collab>. <source>Source</source>. <publisher-loc>Location</publisher-loc>: <publisher-name>Name</publisher-name>; <month>Jul</month> <year>2020</year>. [comment].
<uri xlink:href="https://www.google.com" xmlns:xlink="http://www.w3.org/1999/xlink">URL</uri>
</mixed-citation>
</ref>
合作。来源。地点:姓名;2020年7月。[评论]。
统一资源定位地址
到目前为止,我成功地构建了这个半功能XSLT,它复制了所有节点值,保留了空格和标点符号,还删除了两个子节点“month”和“uri”:
我想创建简单的输出HTML文件,如下所示:
<html>
<p>
<p>Collab. Source. Location: Name; 2020. [comment].</p>
</p>
</html
<html>
<p>
<p>Collab.Source.Location:Name;2020. [comment].</p>
</p>
</html>
合作。来源。地点:姓名;2020[评论]
您可以将模板集压缩为以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="ref">
<html>
<p>
<xsl:apply-templates/>
</p>
</html>
</xsl:template>
<xsl:template match="ref/mixed-citation">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="mixed-citation/*/text() | mixed-citation/text()[last()]">
<xsl:value-of select='normalize-space(.)'/>
</xsl:template>
<xsl:template match="mixed-citation/text()[position() != last()]">
<xsl:value-of select='.'/>
</xsl:template>
<xsl:template match="ref//(month|uri)" />
</xsl:stylesheet>
此解决方案不会使输出加倍。
如果这是您真正想要的,而不是错误,则必须将
模板中的翻一番。感谢您提供的解决方案。它部分工作,请检查上面的更新。也许我一开始还不够清楚,xslt应该始终保留相应的空格和标点符号,不管删除了哪些元素。这在一般情况下是无法解决的,这就是为什么:Jul
在“Jul”之后需要额外的空格,而URL
在“Jul”之前需要额外的空格“URL。因此,这不仅仅是关于保留空间,而是关于根据特定条件添加/删除空间。保留所有空间可以通过
`轻松实现。我对上面的间距问题考虑了很多,最后,如果没有特殊条件,这个问题似乎是无法解决的。谢谢你的回答!目标格式是HTML吗?您需要在输出HTML的呈现或输出HTML的源代码中显示空白吗?考虑到HTML用户代理压缩空白,用纯文本呈现p
似乎与例如foo-bar
和foo-bar
没有区别。因此,似乎只需使用内置的纯文本复制行为,以及用于需要转换为HTML元素的元素的模板,就可以在呈现的HTML中提供所需的输出。如果可能,我希望在HTML源代码中也有正确的空格。所以,是的,在这种情况下,有foo-bar
会很好。然而,这是一个好的想法。呈现的HTML输出在每种情况下都是相同的。此外,最后每个解析的引用都是明文,标记为
元素,没有其他内容。实际上,在上面的例子中,我只是想知道为什么代码>被解析为。有一个尾随空格,但在
之间,空格消失了?有没有其他方法我应该使用?我想得越多,事情就越复杂。。。
<p>Collab. Source. Location: Name; Jul2020. [comment].URL</p>
<p>Collab. Source. Location: Name; Jul 2020. [comment]. URL</p>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="ref">
<html>
<p>
<xsl:apply-templates/>
</p>
</html>
</xsl:template>
<xsl:template match="ref/mixed-citation">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>
<xsl:template match="mixed-citation/*/text() | mixed-citation/text()[last()]">
<xsl:value-of select='normalize-space(.)'/>
</xsl:template>
<xsl:template match="mixed-citation/text()[position() != last()]">
<xsl:value-of select='.'/>
</xsl:template>
<xsl:template match="ref//(month|uri)" />
</xsl:stylesheet>
<!DOCTYPE HTML>
<html>
<p>
<p>Collab. Source. Location: Name; 2020. [comment].</p>
</p>
</html>