我有这样的XML结构,我需要打印出这两个段落所包含的内容。怎么做?基本上,我考虑的是每一个循环,但是在xsl:value中应该放入什么构造呢?谢谢!
<slideshow>
<slide id="A1">
<title>XML techniques</title>
<paragraph> Slideshow prepresents different kind of <bold>XML</bold> techniques </paragraph>
<paragraph> Most common XML Techniques are </paragraph>发布于 2015-09-25 16:59:19
假设您的XSLT看起来像
<xsl:for-each select="//paragraph">
???
</xsl:for-each>你可以写:
<xsl:for-each select="//paragraph">
<xsl:copy-of select="node()"/>
</xsl:for-each>..。这将返回作为段落子节点的节点(文本和元素)的副本。
根据您拥有和想要执行的其他规则,您还可以编写:
<xsl:for-each select="//paragraph">
<xsl:apply-templates select="node()"/>
</xsl:for-each>..。这还会返回一个节点的副本--文本和元素--它们是段落的子节点,除非有其他模板覆盖该行为。
如果您只需要每个段落中的原始文本(即没有粗体标记),则可以使用value-of。
<xsl:for-each select="//paragraph">
<xsl:value-of select="."/>
</xsl:for-each>如果这就是你要做的,你甚至可以写成:
<xsl:value-of select="//paragraph"/>(注意:我举//段为例,因为没有提供上下文,但您可能希望浏览幻灯片并选择段落的子部分)。
发布于 2015-09-26 15:16:48
你写道:
基本上我考虑过每一个循环,
在处理节点时,很少需要xsl:for-each。使用xsl:apply-templates选择所需的节点。如果没有匹配的模板,默认情况下,这将显示节点(文本)的值:
<xsl:template match="slide">
<!-- just process selection of children -->
<xsl:apply-templates select="paragraph" />
</xsl:template>
<!-- add this in case you already have an identity template -->
<xsl:template match="paragraph">
<!-- select only the immediate text children (without <b> for instance) -->
<xsl:value-of select="text()" />
<!-- OR: select the value, incl. all children (using "node()" is equiv.) -->
<xsl:value-of select="." />
</xsl:template>你还写道:
但是,在xsl:value-of construction中应该放什么呢?谢谢!
这在很大程度上取决于焦点。焦点通常由第一个祖先指令xsl:template或xsl:for-each设置。假设您的焦点是<slideshow>,则表达式如下:
<xsl:value-of select="slide/paragraph" />如果焦点已经放在paragraph上,您可以使用select="text()" (选择所有文本子节点,但不是更深),或者使用select="." (选择当前节点,也接受子节点的值)。
但请参阅上文,以获得更有弹性的方法。使用apply-模板可以更容易地为更改和可维护性编写代码。
https://stackoverflow.com/questions/32787125
复制相似问题