如何使用XSLT在父元素中使用子元素属性值?

时间:2015-03-23 12:11:08

标签: xslt

输入XML文件:

<ce:index-entry id="ie0006">
  <ce:index-heading>Acceptance test</ce:index-heading>
  <ce:intra-ref id="ia0011" xlink:href="pii:B978-0-12-801507-0.00017-1#st0040">452–455</ce:intra-ref>
  <ce:intra-ref id="ia0012" xlink:href="pii:B978-0-12-801507-0.00017-1#sp0020">453<ce:italic>f</ce:italic></ce:intra-ref>
  <ce:intra-ref id="ia0013" xlink:href="pii:B978-0-12-801507-0.00017-1#sp0025">455<ce:italic>f</ce:italic></ce:intra-ref>
  <ce:intra-ref id="ia0014" xlink:href="pii:B978-0-12-801507-0.00017-1#st0055">457–458</ce:intra-ref>
</ce:index-entry>
...

输出文件:

    \item Acceptance test\pid{idxB978-0-12-801507-0.00017-1.st0040<tab>00017}
    \pid{idxB978-0-12-801507-0.00017-1.sp0020<tab>00017}
    \pid{idxB978-0-12-801507-0.00017-1.sp0025<tab>00017}
    \pid{idxB978-0-12-801507-0.00017-1.st0055<tab>00017}, 
    \hyperpage{452-455}, \hyperpage{453}, \hyperpage{455}, \hyperpage{457-458}

尝试更新:

<xsl:template match="ce:index-sec/ce:index-entry">
    <xsl:text disable-output-escaping="yes">&#10;\item </xsl:text>
    <xsl:apply-templates select="ce:index-heading"/>

更新自:

       <xsl:for-each select="ce:intra-ref">
      <xsl:text>\pid{idx</xsl:text><xsl:value-of select="@xlink:href"/><xsl:text>}</xsl:text>
      <xsl:if test="position()=last()">
         <xsl:text disable-output-escaping="yes">, </xsl:text>        
      </xsl:if>
       </xsl:for-each>

更新至:

    <xsl:call-template name="do-entries"/>
</xsl:template>

现在OUTPUT似乎是:

\item Acceptance test\pid{idxpii:B978-0-12-801507-0.00017-1#st0040}
\pid{idxpii:B978-0-12-801507-0.00017-1#sp0020}
\pid{idxpii:B978-0-12-801507-0.00017-1#sp0025}
\pid{idxpii:B978-0-12-801507-0.00017-1#st0055}, 452–455, 453$f$, 455$f$, 457–458

我需要将值 pii:B978-0-12-801507-0.00017-1#st0040 更改为 idxB978-0-12-801507-0.00017-1.st0040&lt; tab&gt ; 00017

任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

这将按照您的要求进行

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ce="http:/example.com/ce"
  xmlns:xlink="http:/example.com/xlink"
  version="1.0">

  <xsl:template match="*">
    <xsl:apply-templates select="*" />
  </xsl:template>

  <xsl:template match="ce:index-entry">
    <xsl:text disable-output-escaping="yes">&#10;\item </xsl:text>
    <xsl:apply-templates select="ce:index-heading"/>
    <xsl:apply-templates select="ce:intra-ref"/>
    <xsl:apply-templates select="ce:intra-ref">
      <xsl:with-param name="hyperpage" select="1" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="ce:index-heading">
    <xsl:value-of select="." />
  </xsl:template>

  <xsl:template match="ce:intra-ref">
    <xsl:param name="hyperpage" select="0" />

    <xsl:choose>
      <xsl:when test="$hyperpage = 0">

        <xsl:variable name="pid" select="concat('idx', substring-after(@xlink:href, ':'))" />

        <xsl:variable name="pre" select="substring-before($pid, '#')" />

        <xsl:variable name="post" select="substring-after($pid, '#')" />

        <xsl:variable name="f5">
          <xsl:call-template name="field-n">
            <xsl:with-param name="text" select="$pre"/>
            <xsl:with-param name="n" select="5"/>
          </xsl:call-template>
        </xsl:variable>

        <xsl:text>\pid{</xsl:text>
        <xsl:value-of select="concat($pre, '.', $post)" />
        <xsl:text>&#x09;</xsl:text>
        <xsl:value-of select="substring-after($f5, '.')"/>
        <xsl:text>}&#x0A;</xsl:text>

      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select="concat('\hyperpage{', child::text(), '}')" />
        <xsl:if test="following-sibling::ce:intra-ref">
          <xsl:text>, </xsl:text>
        </xsl:if>
      </xsl:otherwise>

    </xsl:choose>

  </xsl:template>

  <xsl:template match="text/text()" name="field-n">
    <xsl:param name="text"/>
    <xsl:param name="n"/>
    <xsl:choose>
      <xsl:when test="$n = 1">
          <xsl:value-of select="substring-before($text, '-')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="field-n">
          <xsl:with-param name="text" select="substring-after($text, '-')"/>
          <xsl:with-param name="n" select="$n - 1"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

<强>输出

<?xml version="1.0" encoding="utf-8"?>
\item Acceptance test\pid{idxB978-0-12-801507-0.00017-1.st0040  00017}
\pid{idxB978-0-12-801507-0.00017-1.sp0020   00017}
\pid{idxB978-0-12-801507-0.00017-1.sp0025   00017}
\pid{idxB978-0-12-801507-0.00017-1.st0055   00017}
\hyperpage{452–455}, \hyperpage{453}, \hyperpage{455}, \hyperpage{457–458}
相关问题