繁体   English   中英

xslt 中列表元素内的分隔斜体和跨度标签

[英]Separated italic and span tags inside list element in xslt

我是 XSLT 世界的新手,提前感谢您的理解。 我需要准备一个 xml 将发送到 Adobe InDesign 服务器。 在 html 文件中,这是我需要转换为 xml 并使用 Z139E293A7146F23“CD247C6BE049EBF26Z 转换”发送到 Adobe InDesign 的输入,我有“ipan”(内部)标签和“liital”元素。 我想将“i”标签处理为 InDesign 的最终 xml 中的斜体。 我尝试通过以下 xslt 匹配“i”标签:

<xsl:template match="i" mode="process-text">
      <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
        <Content>      
           <xsl:copy-of select="text()"/>
        </Content>
    </CharacterStyleRange>
</xsl:template>

但没有结果。

例如,我有以下输入:

<li class="MsoNormal" style="mso-list:l0 level2 lfo1;tab-stops:list 1.0in">Systolic dysfunction: an&#xa0;<i>inotropic</i>&#xa0;abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction &lt;45%).</li>

我想将其转换为以下内容:

<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL2">
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
            <Content>Systolic dysfunction: an </Content>
            <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
                <Content>inotropic</Content>
            </CharacterStyleRange>
        <Content> abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction &lt;45%).</Content>
            <Br/>
         </CharacterStyleRange>
   </ParagraphStyleRange>

我最初的问题是如何拆分“li”标签并(单独)处理里面的文本,并通过 XSLT 分别处理“li”内的“span”和“i”标签? 预先感谢您的任何帮助。

更新:我的主要模板,“li”元素是:

<xsl:template match="li[not(descendant::p) and not(ancestor::section[@class='references' or @class='References'])]" mode="li-pass1">    
       <xsl:variable name="depth" select="count(ancestor::li) + 1"/>
    
    <xsl:variable name="listType">
      <xsl:choose>
        <xsl:when test="parent::ol">
          <xsl:value-of select="'NL'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'BL'"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
      <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/{$listType}{if ($depth eq 1) then '' else $depth}">          
      <xsl:choose>
        <xsl:when test="descendant::i/text()">
          <Content>      
             <xsl:copy-of select="./text() | descendant::span/text() "/>
          </Content>
      <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
              <Content>      
                  <xsl:copy-of select="descendant::i/text()"/>
              </Content>
          </CharacterStyleRange>
        </xsl:when>
        <xsl:otherwise>
          <Content>      
             <xsl:copy-of select="./text() | descendant::span/text() "/>
          </Content>
        </xsl:otherwise>
      </xsl:choose>
      </ParagraphStyleRange>
    </xsl:template>

此模板以错误的方式影响最终的 xml。 我得到以下结果:

<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL">
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
            <Content>Two potential pathophysiologic conditions lead to the clinical findings of HF, namely systolic and/or diastolic heart dysfunction. 
          </Content>
         </CharacterStyleRange>
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
            <Content>inotropiccompliance</Content>
         </CharacterStyleRange>
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"/>
      </ParagraphStyleRange>
      <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL2">
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
            <Content>Systolic dysfunction: an  abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction &lt;45%).</Content>
         </CharacterStyleRange>
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
            <Content>inotropic</Content>
         </CharacterStyleRange>
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]"/>
      </ParagraphStyleRange>

因此,您可以看到,斜体元素位于单独的标签中,但没有其他内容。 你能建议我需要做什么吗?

我会尝试编写将每个元素类型映射到相应结果结构的模板,并在内部使用<xsl:apply-templates/>来保持处理。 所以该样本的基本方法看起来像

<xsl:template match="li">
    <xsl:variable name="depth" select="count(ancestor::li) + 1"/>
    
    <xsl:variable name="listType">
      <xsl:choose>
        <xsl:when test="parent::ol">
          <xsl:value-of select="'NL'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'BL'"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
     <ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/{$listType}{if ($depth eq 1) then '' else $depth}">
         <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
           <xsl:apply-templates/>
         </CharacterStyleRange>   
     </ParagraphStyleRange>
</xsl:template>

<xsl:template match="i">
      <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
        <Content>      
           <xsl:apply-templates/>
        </Content>
    </CharacterStyleRange>
</xsl:template>

<xsl:template match="text()[normalize-space()]">
    <Content>
        <xsl:value-of select="."/>
    </Content>
</xsl:template>

https://xsltfiddle.liberty-development.net/93dFK9Q

这给了

<ParagraphStyleRange AppliedParagraphStyle="ParagraphStyle/BL">
   <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/$ID/[No character style]">
      <Content>Systolic dysfunction: an </Content>
      <CharacterStyleRange AppliedCharacterStyle="CharacterStyle/Italic">
         <Content>
            <Content>inotropic</Content>
         </Content>
      </CharacterStyleRange>
      <Content> abnormality, due to myocardial infarction (MI) or dilated or ischemic cardiomyopathy (CM), resulting in diminished systolic emptying (ejection fraction &lt;45%).</Content>
   </CharacterStyleRange>
</ParagraphStyleRange>

我可能没有捕获您需要的 output 格式的所有详细信息,但我希望示例表明关键是使用apply-templates来处理具有匹配模板的子节点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM