我的输入xml就像,
<section level="2">
<title>1. Wound Healing<target id="c001"/><target id="page3"/></title>
<figure id="c001_f001" counter="yes">
<legend><para><emph type="bold">Fig. 1-1</emph> Hypertrophic scar.</para></legend>
<subfigure>
<graphic position="center" fileref="images/9781626237896_c001_f001.jpg"/>
</subfigure>
</figure>
<figure counter="yes">
<legend><para><emph type="bold">Fig. 1-2</emph> Keloid scar.</para></legend>
<subfigure>
<graphic id="c001_f002" position="center" fileref="images/9781626237896_c001_f002.jpg"/>
</subfigure>
</figure>
........
</section>所需产出如下:
<section level="2">
<title>1. Wound Healing<target id="c001"/><target id="page3"/></title>
<figure counter="yes">
<legend><para><emph type="bold">Fig. 1-1</emph> Hypertrophic scar.</para> </legend>
<subfigure id="c001_f001">
<graphic position="center" fileref="images/9781626237896_c001_f001.jpg"/>
</subfigure>
</figure>
<figure counter="yes">
<legend><para><emph type="bold">Fig. 1-2</emph> Keloid scar.</para></legend>
<subfigure id="c001_f002">
<graphic position="center" fileref="images/9781626237896_c001_f002.jpg"/>
</subfigure>
</figure>
........
</section>我的xslt就像,
<xsl:template match="subfigure">
<xsl:variable name="fig1" select="parent::figure/@id"></xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id">
<xsl:value-of select="$fig1"></xsl:value-of>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
<xsl:when test="subfigure/@id[not(contains(.,'c'))]">
<xsl:if test="subfigure/@id[not(contains(.,'c'))]">
<xsl:variable name="fig1" select="graphic/@id"></xsl:variable>
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:attribute name="id">
<xsl:value-of select="$fig1"></xsl:value-of>
</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>上面提到的XSLT只用于将图id移到子图元素中。但是我们无法将图形id移动到子图元素中。同样,我们需要删除元素中出现在"1“之后的双空间。数。你能指引我们吗。
发布于 2017-10-26 10:31:05
从另一篇文章(XSLT - To keep parent attribute value only on first child element)中的解决方案开始,您可以应用相同的方法,正好相反:
<!-- identity copy -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<!-- remove id from figure when 1st subfigure does not have id -->
<xsl:template match="figure[descendant::subfigure[not(@id)]]/@id"/>
<!-- insert id from figure into subfigure -->
<xsl:template match="subfigure[not(@id)][ancestor::figure[1]/@id][1]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="ancestor::figure[1]/@id"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- remove id from graphic when superordinate subfigure does not have id -->
<xsl:template match="graphic[ancestor::subfigure[not(@id)]]/@id"/>
<!-- insert id from graphic into subfigure -->
<!-- (set priority="+1" or "-1" to avoid possible conflicts with other template) -->
<xsl:template match="subfigure[not(@id)][descendant::graphic[1]/@id]">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="descendant::graphic[1]/@id"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<!-- replace double whitespace in title -->
<xsl:template match="title[contains(., ' ')]/text()">
<xsl:value-of select="replace(., ' ', ' ')"/>
</xsl:template>对于<title>中的空格控制,您可以直接处理文本节点(如果需要在其他元素上展开XPath表达式)。
https://stackoverflow.com/questions/46946691
复制相似问题