简体   繁体   English

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

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

I am new in XSLT world, thank you in advance for your understending.我是 XSLT 世界的新手,提前感谢您的理解。 I need to prepare a xml which will be send to Adobe InDesign server.我需要准备一个 xml 将发送到 Adobe InDesign 服务器。 In the html files, which are my input that I need to transform to xml and send to Adobe InDesign by using XSLT transformation, I have "li" elements that have "span" tags and "i" (italic) tags inside.在 html 文件中,这是我需要转换为 xml 并使用 Z139E293A7146F23“CD247C6BE049EBF26Z 转换”发送到 Adobe InDesign 的输入,我有“ipan”(内部)标签和“liital”元素。 I would like to treat "i" tags, to be italics in the final xml for InDesign.我想将“i”标签处理为 InDesign 的最终 xml 中的斜体。 I tried to match "i" tags by the following xslt:我尝试通过以下 xslt 匹配“i”标签:

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

but without results.但没有结果。

For example, I have the following input:例如,我有以下输入:

<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>

I would like to transform it to the following one:我想将其转换为以下内容:

<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>

My initial problem is how to split a "li" tag and treat (separately) the text inside, and also treat separately "span" and "i" tags inside "li" by XSLT?我最初的问题是如何拆分“li”标签并(单独)处理里面的文本,并通过 XSLT 分别处理“li”内的“span”和“i”标签? Thank you in advance for any help.预先感谢您的任何帮助。

UPDATE: My main template, for "li" elements is:更新:我的主要模板,“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>

This template affects final xml in a wrong way.此模板以错误的方式影响最终的 xml。 I got the following result:我得到以下结果:

<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>

So, you can see, italic elements are in a separate tag, but without other content.因此,您可以看到,斜体元素位于单独的标签中,但没有其他内容。 Could you please suggest what I need to do?你能建议我需要做什么吗?

I would try to write templates mapping each element type to the corresponding result structure and inside use <xsl:apply-templates/> to keep processing up.我会尝试编写将每个元素类型映射到相应结果结构的模板,并在内部使用<xsl:apply-templates/>来保持处理。 So the basic approach for that sample would look like所以该样本的基本方法看起来像

<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 https://xsltfiddle.liberty-development.net/93dFK9Q

That gives这给了

<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>

I might not have captured all details of your needed output format but I hope the sample shows that the key is to use apply-templates to process child nodes with matching templates.我可能没有捕获您需要的 output 格式的所有详细信息,但我希望示例表明关键是使用apply-templates来处理具有匹配模板的子节点。

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

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