Xml XSLT规范化空白,但保留内部HTML
我正在尝试使用XSLT将XML转换为明文文件,以便加载到数据库中。然而,我需要的其中一个元素可能包含需要保留的HTML格式的文本,以及不需要保留的换行符和空格。我也不想要XML名称空间 该文件比较大,也比较复杂,但下面的示例应该说明这个问题 XML:Xml XSLT规范化空白,但保留内部HTML,xml,xslt,Xml,Xslt,我正在尝试使用XSLT将XML转换为明文文件,以便加载到数据库中。然而,我需要的其中一个元素可能包含需要保留的HTML格式的文本,以及不需要保留的换行符和空格。我也不想要XML名称空间 该文件比较大,也比较复杂,但下面的示例应该说明这个问题 XML: 这是带有标记的文本 这是带有更多标记的文本 也需要没有标记的文本 期望输出: <p>This is text with markup</p><p>This is text with <i>more
这是带有标记的文本
这是带有更多标记的文本
也需要没有标记的文本
期望输出:
<p>This is text with markup</p><p>This is text with <i>more</i> markup</p>
Need text with no markup also
这是带有标记的文本这是带有更多标记的文本
也需要没有标记的文本
对于文本的输出格式,normalize-space()清除所有的换行符和空白,但也删除标记
我尝试过使用xml输出和xsl:copy of,但这会留下换行符、名称空间和字符编码我的一些其他输出(&
->&;
),这是不可取的
提前感谢您的任何想法 在不删除元素的情况下删除空白的关键是正确使用模板,只删除文本节点中的空白,而不是整个元素中的空白 我不是100%清楚您的要求,但这至少应该非常接近:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m2="urn:site-org:v3/m2">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes" />
<!-- Remove any whitespace between elements -->
<xsl:strip-space elements="*" />
<xsl:template match="m2:text">
<xsl:apply-templates />
<!-- Newline -->
<xsl:text>
</xsl:text>
</xsl:template>
<!-- Copy elements beneath text elements, without their namespace-->
<xsl:template match="m2:text//*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<!-- Copy attributes beneath text elements-->
<xsl:template match="m2:text//@*">
<xsl:copy />
</xsl:template>
<!-- Text nodes in HTML content - normalize space but escape entities -->
<xsl:template match="m2:text[.//*]//text()" priority="5">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<!-- Text nodes in HTML content - normalize space and don't escape entities -->
<xsl:template match="m2:text//text()">
<xsl:value-of select="normalize-space()" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
;
在以下输入上运行时:
<outer xmlns="urn:site-org:v3/m2" >
<inner>
<text>
<p class="snazzy">This is text with markup and &&& ampersands</p>
<p>This is text with <i>more</i> markup</p>
</text>
</inner>
<inner>
<text>
Need text with no markup also and some &&& ampersands
</text>
</inner>
</outer>
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要没有标记的文本和一些&&&;符号
结果是:
<p class="snazzy">This is text with markup and &&& ampersands</p><p>This is text with<i>more</i>markup</p>
Need text with no markup also and some &&& ampersands
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要不带标记的文本和一些&&&符号
在不删除元素的情况下删除空白的关键是正确使用模板,只删除文本节点中的空白,而不是整个元素中的空白
我不是100%清楚您的要求,但这至少应该非常接近:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m2="urn:site-org:v3/m2">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes" />
<!-- Remove any whitespace between elements -->
<xsl:strip-space elements="*" />
<xsl:template match="m2:text">
<xsl:apply-templates />
<!-- Newline -->
<xsl:text>
</xsl:text>
</xsl:template>
<!-- Copy elements beneath text elements, without their namespace-->
<xsl:template match="m2:text//*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<!-- Copy attributes beneath text elements-->
<xsl:template match="m2:text//@*">
<xsl:copy />
</xsl:template>
<!-- Text nodes in HTML content - normalize space but escape entities -->
<xsl:template match="m2:text[.//*]//text()" priority="5">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<!-- Text nodes in HTML content - normalize space and don't escape entities -->
<xsl:template match="m2:text//text()">
<xsl:value-of select="normalize-space()" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
;
在以下输入上运行时:
<outer xmlns="urn:site-org:v3/m2" >
<inner>
<text>
<p class="snazzy">This is text with markup and &&& ampersands</p>
<p>This is text with <i>more</i> markup</p>
</text>
</inner>
<inner>
<text>
Need text with no markup also and some &&& ampersands
</text>
</inner>
</outer>
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要没有标记的文本和一些&&&;符号
结果是:
<p class="snazzy">This is text with markup and &&& ampersands</p><p>This is text with<i>more</i>markup</p>
Need text with no markup also and some &&& ampersands
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要不带标记的文本和一些&&&符号
在不删除元素的情况下删除空白的关键是正确使用模板,只删除文本节点中的空白,而不是整个元素中的空白
我不是100%清楚您的要求,但这至少应该非常接近:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m2="urn:site-org:v3/m2">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes" />
<!-- Remove any whitespace between elements -->
<xsl:strip-space elements="*" />
<xsl:template match="m2:text">
<xsl:apply-templates />
<!-- Newline -->
<xsl:text>
</xsl:text>
</xsl:template>
<!-- Copy elements beneath text elements, without their namespace-->
<xsl:template match="m2:text//*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<!-- Copy attributes beneath text elements-->
<xsl:template match="m2:text//@*">
<xsl:copy />
</xsl:template>
<!-- Text nodes in HTML content - normalize space but escape entities -->
<xsl:template match="m2:text[.//*]//text()" priority="5">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<!-- Text nodes in HTML content - normalize space and don't escape entities -->
<xsl:template match="m2:text//text()">
<xsl:value-of select="normalize-space()" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
;
在以下输入上运行时:
<outer xmlns="urn:site-org:v3/m2" >
<inner>
<text>
<p class="snazzy">This is text with markup and &&& ampersands</p>
<p>This is text with <i>more</i> markup</p>
</text>
</inner>
<inner>
<text>
Need text with no markup also and some &&& ampersands
</text>
</inner>
</outer>
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要没有标记的文本和一些&&&;符号
结果是:
<p class="snazzy">This is text with markup and &&& ampersands</p><p>This is text with<i>more</i>markup</p>
Need text with no markup also and some &&& ampersands
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要不带标记的文本和一些&&&符号
在不删除元素的情况下删除空白的关键是正确使用模板,只删除文本节点中的空白,而不是整个元素中的空白
我不是100%清楚您的要求,但这至少应该非常接近:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m2="urn:site-org:v3/m2">
<xsl:output method="xml" indent="no" omit-xml-declaration="yes" />
<!-- Remove any whitespace between elements -->
<xsl:strip-space elements="*" />
<xsl:template match="m2:text">
<xsl:apply-templates />
<!-- Newline -->
<xsl:text>
</xsl:text>
</xsl:template>
<!-- Copy elements beneath text elements, without their namespace-->
<xsl:template match="m2:text//*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<!-- Copy attributes beneath text elements-->
<xsl:template match="m2:text//@*">
<xsl:copy />
</xsl:template>
<!-- Text nodes in HTML content - normalize space but escape entities -->
<xsl:template match="m2:text[.//*]//text()" priority="5">
<xsl:value-of select="normalize-space()"/>
</xsl:template>
<!-- Text nodes in HTML content - normalize space and don't escape entities -->
<xsl:template match="m2:text//text()">
<xsl:value-of select="normalize-space()" disable-output-escaping="yes"/>
</xsl:template>
</xsl:stylesheet>
;
在以下输入上运行时:
<outer xmlns="urn:site-org:v3/m2" >
<inner>
<text>
<p class="snazzy">This is text with markup and &&& ampersands</p>
<p>This is text with <i>more</i> markup</p>
</text>
</inner>
<inner>
<text>
Need text with no markup also and some &&& ampersands
</text>
</inner>
</outer>
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要没有标记的文本和一些&&&;符号
结果是:
<p class="snazzy">This is text with markup and &&& ampersands</p><p>This is text with<i>more</i>markup</p>
Need text with no markup also and some &&& ampersands
这是带有标记和&&&;符号
这是带有更多标记的文本
还需要不带标记的文本和一些&&&符号
拥有有什么不可取之处&代码>?强烈建议在HTML中使用编码实体。它位于数据库中的另一个字段中,该字段不是HTML。好的,您是说
元素可以包含HTML或普通文本吗?它应该如何区分这两者?通过元素的存在?是的。否则,父元素是相同的代码>?强烈建议在HTML中使用编码实体。它位于数据库中的另一个字段中,该字段不是HTML。好的,您是说
元素可以包含HTML或普通文本吗?它应该如何区分这两者?通过元素的存在?是的。否则,父元素是相同的代码>?强烈建议在HTML中使用编码实体。它位于数据库中的另一个字段中,该字段不是HTML。好的,您是说
元素可以包含HTML或普通文本吗?它应该如何区分这两者?通过元素的存在?是的。否则,父元素是相同的代码>?高度推荐在HTML中使用编码实体。它位于数据库的另一个字段中,该字段不是HTML。好的,您是说
元素