代码之家  ›  专栏  ›  技术社区  ›  Ace

xsl:删除图像路径上的前面的文件夹

  •  2
  • Ace  · 技术社区  · 15 年前

    我想删除前面的“./”和第一个文件夹(如果它没有命名为“截图”)。

    所以从

    <image href="./folderx/screenshots/page1.png">
    <image href="./screenshots/page2.png"/>
    <image href="./views/screenshots/table/screenshots/page3.png"/>
    

    <image href="screenshots/page1.png">
    <image href="screenshots/page2.png"/>
    <image href="screenshots/table/screenshots/page3.png"/>
    

    下面的解决方案是这样做的:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE reference
      PUBLIC "-//OASIS//DTD DITA Composite//EN" "http://docs.oasis-open.org/dita/v1.1/OS/dtd/reference.dtd">
    <reference id="sample">
       <title>Sample Title</title>
       <refbody>
          <section>
             <title>Breadcrumb</title>
             <p>
                <xref href=""/>  
                <xref href=""/>
                <xref href=""/>
             </p>
          </section>
          <section>
             <title>Screenshot</title>
             <p id="Typ1e">
                <image href="./views/screenshots/Type1.png" placement="break"/>
             </p>
             <p id="Type2">
                <image href="./views/screenshots/Type2.png" placement="break"/>
             </p>
          </section>
       </refbody>
    </reference>
    

    到:

    <reference xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" id="sample"
               ditaarch:DITAArchVersion="1.2"
               domains="(topic concept)                            (topic concept glossentry)                            (topic concept glossgroup)                            (topic reference)                            (topic task)                            (topic hi-d)                             (topic ut-d)                             (topic indexing-d)                            (topic hazard-d)                            (topic abbrev-d)                            (topic pr-d)                             (topic sw-d)                            (topic ui-d)                             (topic task strictTaskbody-c)    "
               class="- topic/topic       reference/reference ">
       <title class="- topic/title ">Sample Title</title>
       <refbody class="- topic/body        reference/refbody ">
          <section class="- topic/section ">
             <title class="- topic/title ">Breadcrumb</title>
             <p class="- topic/p ">
                <xref href="" class="- topic/xref "/>  
                <xref href="" class="- topic/xref "/>
                <xref href="" class="- topic/xref "/>
             </p>
          </section>
          <section class="- topic/section ">
             <title class="- topic/title ">Screenshot</title>
             <p id="Typ1e" class="- topic/p ">
                <image href="screenshots/Type1.png" placement="break" class="- topic/image "/>
             </p>
             <p id="Type2" class="- topic/p ">
                <image href="screenshots/Type2.png" placement="break" class="- topic/image "/>
             </p>
          </section>
       </refbody>
    </reference>
    

    使用XSLT:

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match=
            "image/@href[starts-with(.,'./')
            and not(starts-with(.,'./screenshots/'))
            ]">
            <xsl:attribute name="href">
                <xsl:value-of select="substring-after(substring-after(.,'./'), '/')"/>
            </xsl:attribute>
        </xsl:template>
    
        <xsl:template match="image/@href[starts-with(.,'./screenshots/')]">
            <xsl:attribute name="href">
                <xsl:value-of select="substring-after(.,'./')"/>
            </xsl:attribute>
        </xsl:template>
    </xsl:stylesheet>
    

    我不确定这是否相关,但我用的是氧气12.0。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Dimitre Novatchev    15 年前

    更新 :op(@ace)修改了他的问题,并报告下面的转换产生了一些新的附加属性。

    这种影响是由于他正在使用 <!DOCTYPE 关联的DTD为一些元素提供了一些默认属性。

    所以,没有什么新的或令人惊讶的!:)

    当你有一个 <!文档类型 当DTD具有默认属性时,将DTD与文档关联的指令。

    要删除这些属性,如果不需要它们,可以添加带有空主体的匹配模板。 但是,请注意,这可能会使整个结果对DTD无效。 !


    这是对标识规则的机械覆盖:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="node()|@*">
      <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match=
       "image/@href[starts-with(.,'./')
                  and not(starts-with(.,'./screenshots/'))
                    ]">
      <xsl:attribute name="href">
       <xsl:value-of select="substring-after(substring-after(.,'./'), '/')"/>
      </xsl:attribute>
     </xsl:template>
    
     <xsl:template match="image/@href[starts-with(.,'./screenshots/')]">
      <xsl:attribute name="href">
       <xsl:value-of select="substring-after(.,'./')"/>
      </xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    当此转换应用于以下XML文档时 :

    <t>
        <image href="./folderx/screenshots/page1.png"/>
        <image href="./screenshots/page2.png"/>
        <image href="./views/screenshots/table/screenshots/page3.png"/>
    </t>
    

    产生了想要的结果 :

    <t>
        <image href="screenshots/page1.png"></image>
        <image href="screenshots/page2.png"></image>
        <image href="screenshots/table/screenshots/page3.png"></image>
    </t>