向文本添加元素,但删除 in Between 元素



我没有使用 XSLT 很多。但不知何故,我正在努力获得以下项目的预期输出。

输入 1:

<name>xxxx <xsample>dddd</xsample> zzzz</name>

1 的输出:

<p><t>xxxx dddd zzzz</t></p> // here I don't want to wrap the tag

输入 2

<name>xxxx <ysample>dddd</ysample> zzzz</name>

2 的输出:

<p><t>xxxx </t><t>dddd</t><t> zzzz</t></p>

我已经尝试了以下xslt代码:

<xsl:template match="name">
<p>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()|text()"/>
</p>
</xsl:template>
<xsl:template match="name/text()[not(parent::ysample)]">
<t><xsl:value-of select="."/></t>
</xsl:template>
<xsl:template match="name/ysample">
<t><xsl:value-of select="."/></t>
</xsl:template>

有人可以帮我吗?

谢谢 库玛

我认为问题出在这一行上

<xsl:template match="name//text()[not(parent::ysample)]">

这里有两个问题

  1. name/text()匹配作为name直接子节点的文本节点,因此适用于文本节点的条件not(parent::ysample)永远不会为真,因为父节点将始终name
  2. 这可能是一个错别字,但您可能想在此处检查xsample以实现您的逻辑,特别是因为您已经有一个模板匹配ysample

试试这一行:

<xsl:template match="name//text()[not(parent::xsample)]">

您也可以在 XSLT 2.0 中通过分组进行检查

<xsl:template match="name">
<p>
<xsl:for-each-group select="node()" group-adjacent="self::text() or self::xsample">
<t>
<xsl:value-of select="current-group()"/>
</t>
</xsl:for-each-group>            
</p>
</xsl:template>

最新更新