Xslt 基于少量起始文本对文本节点和元素节点进行分组
请建议根据图|图|图|表|表等几种文本格式对文本+元素节点进行分组。如果这些引文文本以括号开始,以括号结束,如,[,{,],}号,分组也应将括号括起来,否则图|表字+外部参照元素将分组在***内 这些分组应适用于除“Refs”元素下的任何文本节点 输入: XSLT2: 所需结果:Xslt 基于少量起始文本对文本节点和元素节点进行分组,xslt,xslt-2.0,Xslt,Xslt 2.0,请建议根据图|图|图|表|表等几种文本格式对文本+元素节点进行分组。如果这些引文文本以括号开始,以括号结束,如,[,{,],}号,分组也应将括号括起来,否则图|表字+外部参照元素将分组在***内 这些分组应适用于除“Refs”元素下的任何文本节点 输入: XSLT2: 所需结果: 这里尝试使用两个步骤,第一个步骤转换任何模式[]?Fig\。| Figs\。| Figure | Table[s]?然后,第二步尝试使用group start with/end with将这些内容包装到col1中: 正
这里尝试使用两个步骤,第一个步骤转换任何模式[]?Fig\。| Figs\。| Figure | Table[s]?然后,第二步尝试使用group start with/end with将这些内容包装到col1中: 正如您在上所看到的,这种方法似乎为您发布的输入生成了所需的结果,但for元素除外
<Para1>The citations are like (Tables <xref refID="t1">1</xref> and <xref refID="t2">2</xref>; Fig. <xref refID="f1">1</xref>).</Para1>
带有元素的混合文本很难处理,因此一种方法通常是两步一步,第一步将作为标记的文本转换为元素,第二步可用于每个组,从生成的标记元素上的/结束或相邻组开始。即使这样,如果内容可以嵌套也不是一件容易的事情。是的,先生,我用2个XSLT和Perl正则表达式实现了这一点。但在某些领域Perl不适用,只需要XSLT脚本;图1。未正确处理,因为有表格以及转换为起始元素的Fig.文本,因此,以开始/结束的组不会给出您发布的结果。这就提出了一个普遍的问题:在同一个父元素中是否有多个表或图形,它们是否总是必须打包在一起?先生,答案很好,在同一个父元素中是否有多个表或图形,它们是否总是必须打包在一起?先生,这些格式非常罕见。非常感谢您解决这个问题。请在答案栏中张贴建议。我需要接受这个伟大的答案。非常感谢你的伟大方法来解决文本节点分组。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>
<xsl:template match="Para">
<xsl:copy><xsl:call-template name="tempCrossRef1"/></xsl:copy>
</xsl:template>
<xsl:template name="tempCrossRef1">
<!--xsl:analyze-string select="." regex="\([ ]+)|([\+])|([=])|([%])|([/])|([\[])|([\]])"-->
<!-- (Fig. <xref refID="f1">1</xref>) -->
<!--xsl:analyze-string select="node()" regex="\(Fig. ">
<xsl:matching-substring>
<xsl:choose>
<xsl:when test="following-sibling::node()[2][parent::*/name()='xref']">
<col><xsl:apply-templates select="."/></col>
</xsl:when>
<xsl:otherwise><xsl:apply-templates select="."/></xsl:otherwise>
</xsl:choose>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string-->
<xsl:for-each select="node()">
<xsl:choose>
<xsl:when test="ends-with(., 'Fig.')">
<xsl:for-each-group select="self::node()[ends-with(., 'Fig.')]" group-adjacent="boolean(self::xref)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()" />
</xsl:when>
<xsl:otherwise>
<p1>
<xsl:apply-templates select="current-group()" />
</p1>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="xref">
<xref>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates />
</xref>
</xsl:template>
</xsl:stylesheet>
<root>
<Para>The citations are like <col1>(Fig. <xref refID="f1">1</xref>)</col1>.</Para>
<Para>The <b>citations are like <col1>(Fig. <xref refID="f1">1</xref>)</col1>.</b></Para>
<Para>The citations are like <col1>(Fig. <xref refID="f1">1</xref>)</col1>.</Para>
<Extract>The citations are like <col1>(Figs. <xref refID="f1">1</xref> and <xref refID="f2">2</xref>)</col1>.</Extract>
<DispQuote>The citations are like <col1>(Tables <xref refID="t1">1</xref> and <xref refID="t2">2</xref>)</col1>.</DispQuote>
<Para1>The citations are like <col1>(Tables <xref refID="t1">1</xref> and <xref refID="t2">2</xref>; Fig. <xref refID="f1">1</xref>)</col1>.</Para1>
<Para2>The citations are like (analysation of <col1>Fig. <xref refID="f1">1</xref></col1>).</Para2>
<Para>The citations are like (explained in <col1>Figs. <xref refID="f1">1</xref> and <xref refID="f2">2</xref></col1>).</Para>
<Para>The citations are like (Chapter 1 and 3 are explained in <col1>Tables <xref refID="t1">1</xref> and <xref refID="t2">2</xref></col1>).</Para>
<Refs>The citations are like (Fig. <xref refID="f1">1</xref>).</Refs><!-- Within this element, grouping not required-->
</root>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:param name="start-patterns" as="xs:string">[(]?(Fig\.|Figs\.|Figure|Table[s]?)</xsl:param>
<xsl:param name="end-patterns" as="xs:string">[)]</xsl:param>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:mode name="text-to-elements" on-no-match="shallow-copy"/>
<xsl:template match="root/*[not(self::Refs)][matches(., $start-patterns)]">
<xsl:copy>
<xsl:variable name="text-to-elements" as="node()*">
<xsl:apply-templates mode="text-to-elements"/>
</xsl:variable>
<xsl:for-each-group select="$text-to-elements" group-starting-with="start">
<xsl:choose>
<xsl:when test="self::start">
<xsl:for-each-group select="current-group()" group-ending-with="end">
<xsl:choose>
<xsl:when test="current-group()[last()][self::end]">
<col1>
<xsl:apply-templates select="current-group()"/>
</col1>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="start | end">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="text()" mode="text-to-elements">
<xsl:analyze-string select="." regex="{$start-patterns}">
<xsl:matching-substring>
<start>
<xsl:value-of select="."/>
</start>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:analyze-string select="." regex="{$end-patterns}">
<xsl:matching-substring>
<end>
<xsl:value-of select="."/>
</end>
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="."/>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
</xsl:stylesheet>
<Para1>The citations are like (Tables <xref refID="t1">1</xref> and <xref refID="t2">2</xref>; Fig. <xref refID="f1">1</xref>).</Para1>