我有以下xml
<Paragraph>
<Span Text="This is " />
<HyperlinkRangeStart AnnotationID="1">
<HyperlinkInfo NavigateUri="http://www.google.com" />
</HyperlinkRangeStart>
<Span StyleName="Hyperlink" Text="Go" />
<Span FontWeight="Bold" StyleName="Hyperlink" Text="o" />
<Span StyleName="Hyperlink" Text="gle" />
<HyperlinkRangeEnd AnnotationID="1" />
<Span Text=" search engine" />
</Paragraph>
我想将其转换为xhtml文本,如下所示:
<p>This is <a href=http://www.google.com> [Go**o**gle][1]</a> search engine</p>
非常感谢任何帮助。
答案 0 :(得分:0)
这不是一件容易的事,所以让我稍微简化一下输入,虽然有多个(我的例子中有两个)超链接:
<强>输入:强>
<Paragraph>
<Span Text="This is " />
<HyperlinkRangeStart AnnotationID="1">
<HyperlinkInfo NavigateUri="http://www.google.com" />
</HyperlinkRangeStart>
<Span StyleName="Hyperlink" Text="Go" />
<Span StyleName="Hyperlink" Text="o" />
<Span StyleName="Hyperlink" Text="gle" />
<HyperlinkRangeEnd AnnotationID="1" />
<Span Text=" search engine. " />
<Span Text="That is " />
<HyperlinkRangeStart AnnotationID="2">
<HyperlinkInfo NavigateUri="http://www.bing.com" />
</HyperlinkRangeStart>
<Span StyleName="Hyperlink" Text="Bi" />
<Span StyleName="Hyperlink" Text="ng" />
<HyperlinkRangeEnd AnnotationID="2" />
<Span Text=" search motor." />
</Paragraph>
应用以下样式表:
XSLT 1.0
<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:key name="URLtext" match="Span[@StyleName='Hyperlink']" use="preceding-sibling::HyperlinkRangeStart[1]/@AnnotationID" />
<xsl:template match="Paragraph">
<p>
<xsl:apply-templates select="Span[not(@StyleName='Hyperlink')] | HyperlinkRangeStart"/>
</p>
</xsl:template>
<xsl:template match="HyperlinkRangeStart">
<a href="{HyperlinkInfo/@NavigateUri}">
<xsl:apply-templates select="key('URLtext', @AnnotationID)"/>
</a>
</xsl:template>
<xsl:template match="Span">
<xsl:value-of select="@Text"/>
</xsl:template>
</xsl:stylesheet>
将返回此结果:
<?xml version="1.0" encoding="utf-8"?>
<p>This is <a href="http://www.google.com">Google</a> search engine. That is <a href="http://www.bing.com">Bing</a> search motor.</p>
格式化超文本链接的问题留给读者练习。