首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT -移动子ID和父ID属性,并删除字符串中数字后面的双空格。

XSLT -移动子ID和父ID属性,并删除字符串中数字后面的双空格。
EN

Stack Overflow用户
提问于 2017-10-26 05:47:13
回答 1查看 90关注 0票数 1

我的输入xml就像,

代码语言:javascript
复制
<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>

所需产出如下:

代码语言:javascript
复制
<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就像,

代码语言:javascript
复制
<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“之后的双空间。数。你能指引我们吗。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-26 10:31:05

从另一篇文章(XSLT - To keep parent attribute value only on first child element)中的解决方案开始,您可以应用相同的方法,正好相反:

代码语言:javascript
复制
<!-- 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表达式)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46946691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档