[英]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.