gpt4 book ai didi

xslt - 如何使用xslt合并元素?

转载 作者:行者123 更新时间:2023-12-03 16:49:37 24 4
gpt4 key购买 nike

我有一个带元素的段落引用类型。



输入文件:

<reference>
<emph type="bold">Antony</emph><emph type="bold">,</emph> <emph type="bold">R.</emph>
<emph type="bold">and</emph> <emph type="bold">Micheal</emph><emph type="bold">,</emph> <emph type="bold">V.</emph>
<emph type="italic">reference title</emph></reference>


现在收到的输出:

<p class="reference"><strong>Antony</strong><strong>,</strong> <strong>R.</strong>
<strong>and</strong> <strong>Micheal</strong><strong>,</emph>
<emph type="bold">V.</strong> <em>reference title></em></p>


所需的输出文件:

<p class="reference"><strong>Antony, R. and Micheal, V.</strong> <em>reference title</em></p>


我的xslt脚本:

<xsl:template match="reference">
<p class="reference"><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="emph">
<xsl:if test="@type='bold'">
<strong><xsl:apply-templates/></strong>
</xsl:if>
<xsl:if test="@type='italic'">
<em><xsl:apply-templates/></em>
</xsl:if>
</xsl:template>


在xslt中需要纠正哪些内容才能像所需的输出文件一样一次性获得 <strong>元素?

请告诉任何人..

通过,
安妮

最佳答案

这是XSLT 1.0解决方案:

<xsl:stylesheet 
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="xml" encoding="utf-8" />

<!-- the identity template copies everything verbatim -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>

<!-- this matches the first <emph> nodes of their kind in a row -->
<xsl:template match="emph[not(@type = preceding-sibling::emph[1]/@type)]">
<xsl:variable name="elementname">
<xsl:choose>
<xsl:when test="@type='bold'">strong</xsl:when>
<xsl:when test="@type='italic'">em</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:if test="$elementname != ''">
<!-- the first preceding node with a different type is the group separator -->
<xsl:variable
name="boundary"
select="generate-id(preceding-sibling::emph[@type != current()/@type][1])
" />
<xsl:element name="{$elementname}">
<!-- select all <emph> nodes of the row with the same type... -->
<xsl:variable
name="merge"
select=". | following-sibling::emph[
@type = current()/@type
and
generate-id(preceding-sibling::emph[@type != current()/@type][1]) = $boundary
]"
/>
<xsl:apply-templates select="$merge" mode="text" />
</xsl:element>
</xsl:if>
</xsl:template>

<!-- default: keep <emph> nodes out of the identity template mechanism -->
<xsl:template match="emph" />

<!-- <emph> nodes get their special treatment here -->
<xsl:template match="emph" mode="text">
<!-- effectively, this copies the text node via the identity template -->
<xsl:apply-templates />

<!-- copy the first following node - if it is a text node
(this is to get interspersed spaces into the output) -->
<xsl:if test="
generate-id(following-sibling::node()[1])
=
generate-id(following-sibling::text()[1])
">
<xsl:apply-templates select="following-sibling::text()[1]" />
</xsl:if>
</xsl:template>

</xsl:stylesheet>


结果是:

<reference>
<strong>Antony, R. and Micheal, V.</strong>
<em>reference title</em>
</reference>




我不太满意

<xsl:variable 
name="merge"
select=". | following-sibling::emph[
@type = current()/@type
and
generate-id(preceding-sibling::emph[@type != current()/@type][1]) = $boundary
]"
/>


如果有人有更好的主意,请告诉我。

关于xslt - 如何使用xslt合并元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1542775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com