2011-09-21 130 views
1

有人能告诉我下面修复的最简单方法吗?我目前有一个文件,其中包含多种方法来定义交叉引用(基本上链接到其他页面),并且我想将其中的2个转换为单一格式。下面的XML是一个简化的样品示出源格式:XSLT字符串操作

<Paras> 
<Para tag="CorrectTag"> 
<local xml:lang="en">Look at this section <XRef XRefType="(page xx)">(page 36)</XRef> for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
<local xml:lang="en">Look at some other section (page <XRef XRefType="xx">52</XRef>) for more information</local> 
</Para> 
</Paras> 

我想要实现的是以下内容:

<Paras> 
<Para tag="CorrectTag"> 
    <local xml:lang="en">Look at this section <XRef XRefType="(page xx)" XRefPage="36"/> for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
    <local xml:lang="en">Look at some other section <XRef XRefType="(page xx)" XRefPage="52"/> for more information</local> 
</Para> 
</Paras> 

使用下面XSLT转换的[外部参照]元素

<xsl:template match="XRef"> 
    <xsl:copy> 
     <xsl:attribute name="XRefType">(page xx)</xsl:attribute> 
     <xsl:choose> 
      <xsl:when test="@XRefType='(page xx)'"> 
       <xsl:attribute name="XRefPage" select="substring-before(substring-after(.,'(page '),')')"/> 
      </xsl:when> 
      <xsl:when test="@XRefType='xx'"> 
       <xsl:attribute name="XRefPage" select="."/> 
      </xsl:when> 
     </xsl:choose> 
    </xsl:copy> 
</xsl:template> 

已经给我这个输出:

<Paras> 
<Para tag="CorrectTag"> 
    <local xml:lang="en">Look at this section<XRef XRefType="(page xx)" XRefPage="36"/>for more information</local> 
</Para> 
<Para tag="InCorrectTag"> 
    <local xml:lang="en">Look at some other section (page<XRef XRefType="(page xx)" XRefPage="52"/>) for more information</local> 
</Para> 
</Paras> 

哪一个已经解决了我的大部分问题,但我一直在坚持如何在不删除太多其他内容的情况下清理[local]元素的其余部分。我需要的是这样的:如果字符串“(page”后面跟着一个XRef元素,然后将其删除,如果字符串“)”前面有一个XRef元素,请将其删除。否则,请勿触摸它们。

有关如何解决这个问题的任何建议?

谢谢!

回答

1

你应该能够解决这个问题,例如模板

<xsl:template match="text()[ends-with(., '(page ')][following-sibling::node()[1][self::XRef]]"> 
    <xsl:value-of select="replace(., '(page $', '')"/> 
</xsl:template> 

<xsl:template match="text()[starts-with(., ')')][preceding-sibling::node[1][self::XRef]"> 
    <xsl:value-of select="substring(., 2)"/> 
</xsl:template> 

当然,您需要确保这些文本节点的父元素的任何模板执行apply-templates来处理子节点。

+0

再次感谢马丁,它的确有窍门。如果有人喜欢重复使用,那么存在一个小的错字:[之前的兄弟姐妹::节点[1] [self :: XRef]]需要[之前的兄弟姐妹::节点()[1] [self :: XRef]] 。 – Wokoman