[英]How do I match all of an element's content with a XSLT?
我有一个XML文件,其中包含一些转义的XHTML,如下所示:
<AuthorBio>
<p>Paragraph with<br/>forced line break.</p>
</AuthorBio>
我正在尝试使用XSLT将其转换为以下内容:
<ParagraphStyleRange>
<CharacterStyleRange>
<Content>
Paragraph with
forced line break.
</Content>
</CharacterStyleRange>
<Br/>
</ParagraphStyleRange>
这是我的XSLT的简化版本:
<xsl:template match="AuthorBio">
<xsl:for-each select="p">
<ParagraphStyleRange>
<xsl:apply-templates select="./node()"/>
<Br/>
</ParagraphStyleRange>
</xsl:for-each>
</xsl:template>
<xsl:template match="AuthorBio/p/text()">
<CharacterStyleRange>
<Content><xsl:value-of select="."/></Content>
</CharacterStyleRange>
</xsl:template>
<xsl:template match="br">


</xsl:template>
不幸的是,这给了我以下结果:
<ParagraphStyleRange>
<CharacterStyleRange>
<Content>
Paragraph with
</Content>
</CharacterStyleRange>


<CharacterStyleRange>
<Content>
forced line break.
</Content>
</CharacterStyleRange>
<Br/>
</ParagraphStyleRange>
我意识到这是因为我的模板与p/text()
匹配,因此在<br/>
中断。 但是,除非我采用完全错误的方法,否则我无法想到一种选择元素的全部内容(包括所有子节点)的方法。 我想类似于copy-of
,除了删除包装元素。 这可能吗? 有没有办法匹配节点的全部内容,而不是节点本身? 还是有更好的方法来解决这个问题?
看起来您只想为每个段落输出一次CharacterStyleRange
和Content
元素。 如果是这样,请将当前与AuthorBio/p/text()
匹配的模板更改为仅与p
匹配,然后在其中输出ParagraphStyleRange
, CharacterStyleRange
和Content
。
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="AuthorBio">
<xsl:apply-templates select="p" />
</xsl:template>
<xsl:template match="p">
<ParagraphStyleRange>
<CharacterStyleRange>
<Content>
<xsl:apply-templates />
</Content>
</CharacterStyleRange>
<Br/>
</ParagraphStyleRange>
</xsl:template>
<xsl:template match="br">
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.