【问题标题】:XSLT, XML: Getting the number of a step or figure for cross-referencesXSLT、XML:获取步骤或图形的编号以进行交叉引用
【发布时间】:2015-03-23 21:31:25
【问题描述】:

我正在使用 XSLT 将 XML 文档(用户手册的一部分)显示为 HTML。对于数字,我有以下 XSLT 模板:

<xsl:template match = "figure">
    <a>
        <xsl:attribute name = "name">
            <xsl:value-of select = "./@id"/>
        </xsl:attribute>
        <p class = "caption">Figure <xsl:number count="figure"/>: <xsl:value-of select="./title"/></p> 
        <p class = "graphic">
            <img style="width:700px;">
                <xsl:attribute name = "src">
                    <xsl:value-of select = "./graphic/@boardno"/>
                </xsl:attribute>
            </img>
        </p>
    </a>
 </xsl:template>

这将显示图形并根据其在文档中的位置为其指定一个编号。现在,我的图形参考模板()是这样的:

<xsl:template match = "figref">
     <a>
     <xsl:attribute name = "href">
         #<xsl:value-of select = "./@IDREF" />
     </xsl:attribute>
     <span class = "figure>
    <xsl:value-of select = "."/>   
     </span>
     </a>
</xsl:template>

这是 XML 本身:

<?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="lorip.xsl"?>
    <descopim docid="LoremIpsumSample" >
        <descwp id ="descwp_LorIp">
            <titleblk>
                   <wptitle>
                    <maintlvl>Dolor Sit Amet</maintlvl>
                    <subject>Natoque Page - Description and Controls</subject>
                    <sysnomen>
                        <name>Lorem Ipsum</name>
                    </sysnomen>
                </wptitle>  
            </titleblk>
<intro>
            <title>Nunc Eget ut Vivamus</title>
            <para id="NuncEgVivpara001">
                Lorem ipsum dolor sit amet, dui eu, est natoque suscipit suspendisse fringilla et. Adipisicing nonummy, urna in justo. 
                Maecenas sollicitudin nam ut a asperiores mollis. Nunc eget ut ut vivamus, sit mollis integer purus amet. Consequat 
                molestie pellentesque, ultricies tincidunt elit arcu nullam diam, in tempor morbi integer.
            </para>
        </intro>
        <sysdesc>
            <title>Nunc Eget Luctus Nullam</title>
            <desc>
                <para>
                    Neque eu maecenas pellentesque metus, gravida convallis est orci
                    <figref IDREF = "ConvEstOrcifig001" label = "Convallis est Orci">Figure </figref> sollicitudin nam ut a asperiores mollis,  
                    <figref IDREF = "AsperMollfig002" label = "Asperiores Mollis">  Figure </figref>.           
                    Amet nostra mus aptent ut commodo. Platea vestibulum vitae non felis, sed ac. Morbi quam tortor vestibulum lorem fringilla 
                    nec. Enim sed taciti dictum vehicula sit, risus at vehicula risus wisi quis, fermentum dolor ante illo quis odio at, 
                    dignissim nullam arcu nisl arcu nec ullamcorper, libero vehicula sed. <extref href = "appendixb.pdf">Appendix B (Adipisicing 
                    Nonummy)</extref> et <extref href = "appendixc.pdf">Appendix C (Maecenas sollicitudin) </extref>.
                </para>
            </desc>
        </sysdesc>
            <figsect>
                <figure id = "SuspFringfig001">
                    <title>Suspendisse Fringilla</title>
                    <graphic boardno = "SuspFringfig001.png"></graphic>
                </figure>
                <figure id = "AsperMollfig002">
                    <title>Asperiores Mollis </title>
                    <graphic boardno = "AsperMollfig002.png"></graphic>
                </figure>
                <figure id = "VivPulLitfig007">
                    <title>Viverra Pulvinar Litora</title>
                    <graphic boardno = "VivPulLitfig007.png"></graphic>
                </figure>
            </figsect>
        </descwp>
    </descopim>

我需要一种方法从图形本身中提取数字(xsl:number count="figure"),或者确定该图形在总体顺序中的数字,以通过交叉引用显示该数字(例如, “图 3”,而不仅仅是“图”。)

所以,以第一个 figref 为例,我正在寻找的结果是:

<a href ="#SuspFringfig001">
  <span class = "figure">
    Figure 1  
  </span>
</a>

【问题讨论】:

  • 您确实需要发布一个小(但完整)的输入示例和预期输出。一般情况下,xsl:number 会“随用随用”为节点编号,并且无法在同一转换中的其他地方引用它 - 因此您将不得不使用其他方法。
  • @michael.hor257k 谢谢。已添加示例。

标签: xml xslt


【解决方案1】:

这个问题有点难以回答,因为您没有minimal, complete and verifiable example

但是,您可以使用模版模板来获取图形编号。只需对具有与当前IDREF 属性匹配的idfigure 执行xsl:apply-templates

这是我正在谈论的一个工作示例......

XML 输入(可能与您的实际输入完全不同,但至少格式正确)

<doc>
    <figure id="fig1">
        <title>Title 1</title>
        <graphic boarno="fig1-g1"/>
    </figure>
    <figure id="fig2">
        <title>Title 2</title>        
        <graphic boarno="fig2-g1"/>
    </figure>
    <figure id="fig3">
        <title>Title 3</title>
        <graphic boarno="fig3-g1"/>
    </figure>
    <para>This should be a ref to figure 1: <figref IDREF="fig1"/></para>
    <para>This should be a ref to figure 2: <figref IDREF="fig2"/></para>
    <para>This should be a ref to figure 3: <figref IDREF="fig3"/></para>
</doc>

XSLT 1.0(您没有指定版本)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="figure">
        <a name="{@id}">
            <p class="caption">Figure <xsl:number/>: <xsl:value-of select="title"/></p> 
            <p class="graphic">
                <img style="width:700px;" src="{graphic/@boarno}"/>                
            </p>
        </a>
    </xsl:template>

    <xsl:template match="figure" mode="fignbr">
        <xsl:text>Figure </xsl:text>
        <xsl:number/>
    </xsl:template>

    <xsl:template match="figref">
        <a href="#{@IDREF}">
            <span class="figure">
                <xsl:apply-templates select="//figure[@id=current()/@IDREF]" mode="fignbr"/>
                <xsl:apply-templates/>
            </span>
        </a>
    </xsl:template>

</xsl:stylesheet>

XML 输出

<doc>
   <a name="fig1">
      <p class="caption">Figure 1: Title 1</p>
      <p class="graphic">
         <img style="width:700px;" src="fig1-g1"/>
      </p>
   </a>
   <a name="fig2">
      <p class="caption">Figure 2: Title 2</p>
      <p class="graphic">
         <img style="width:700px;" src="fig2-g1"/>
      </p>
   </a>
   <a name="fig3">
      <p class="caption">Figure 3: Title 3</p>
      <p class="graphic">
         <img style="width:700px;" src="fig3-g1"/>
      </p>
   </a>
   <para>This should be a ref to figure 1: <a href="#fig1">
         <span class="figure">Figure 1</span>
      </a>
   </para>
   <para>This should be a ref to figure 2: <a href="#fig2">
         <span class="figure">Figure 2</span>
      </a>
   </para>
   <para>This should be a ref to figure 3: <a href="#fig3">
         <span class="figure">Figure 3</span>
      </a>
   </para>
</doc>

您可能还可以使用xsl:key 来更有效地获取数字编号,但如果没有更好的输入/输出/xslt 样本,就很难给出示例。

【讨论】:

    【解决方案2】:

    在 XSLT 1.0 中,要使用 xsl:number 获取节点的编号,它必须是上下文节点。您可以通过使用 xsl:apply-templates 或 xsl:for-each 选择它来使其成为上下文节点。在 XSLT 2.0 中,xsl:number 有一个 select 属性,因此您可以通过选择它来获取任何节点的编号。

    <xsl:template match="figref">
      <xsl:number select="idref(@ref)" count="figure"/>
    </xsl:template>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多