XSLT:读取由空标签划分的内容 [英] XSLT: Reading content that is devided by empty tags
问题描述
所以我正忙着创建一个 XSLT 文件来将各种 XML 文档处理成一个新的节点布局.
So I am busy creating a XSLT file to process various XML documents into a new node layout.
有一件事我想不通,这是我正在使用的 XML 示例:
There's one thing I can't figure out, here is an example of XML that I'm working with:
<page>
This is a paragraph on the page.
<newParagraph/>
This is another paragraph.
<newParagraph/>
Here is yet another paragraph on this page.
<page>
如您所见,段落使用空标签作为分隔符进行分割.在结果 XML 我想要这个:
As you can see the paragraphs are split up using empty tags as deviders. In the result XML I want this:
<page>
<p>
This is a paragraph on the page.
</p>
<p>
This is another paragraph.
</p>
<p>
Here is yet another paragraph on this page.
</p>
<page>
如何使用 XSLT(仅限 1.0 版)实现此目的?
How can I achieve this using XSLT (Version 1.0 only)?
推荐答案
如果你愿意作弊"一点,你可以手动将 XML 标签插入到结果文档中,这些标签不是节点树的一部分而是普通文本.然而,下游处理器不会注意到差异,前提是它重新解析输出.
If you are willing to "cheat" a little bit you can manually insert XML tags into result document which are not part of the node tree but which are normal text. A processor downstream, however, will not notice the difference provided that it re-parses the output.
鉴于我的其他答案的输入,以下 XSLT 1.0 转换将起作用(保留段落中的子树):
Given the input of my other answer the following XSLT 1.0 transformation will do the trick (preserving the sub trees in the paragraphs):
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="page">
<page>
<P>
<xsl:apply-templates/>
</P>
</page>
</xsl:template>
<!-- this default rule recursively copies all substructures within a paragraph at tag level -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- this default rule makes sure that texts between the tags are printed -->
<xsl:template match="text()">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="newParagraph">
<!-- This inserts a matching closing and opening tag -->
<xsl:value-of select="'</P><P>'" disable-output-escaping="yes" />
</xsl:template>
</xsl:stylesheet>
这篇关于XSLT:读取由空标签划分的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!