XSL:删除imageobject fileref上的前一个文件夹

时间:2010-11-19 22:36:47

标签: xml xslt docbook

我想删除前面的'./'第一个文件夹,如果它没有为任何属于imagedata的fileref命名为“screenshots”

所以来自

<section xmlns="http://docbook.org/ns/docbook" version="5">
  <title>Screenshot</title>

  <para xreflabel="New Type" xml:id="manageNewType">
     <mediaobject>
        <imageobject>
           <imagedata fileref="./views/screenshots/manageType1.png" width="100%"/>
        </imageobject>
        <caption>
           <para>New Mode</para>
        </caption>
     </mediaobject>
  </para>

  <para xreflabel="Edit Type" xml:id="manageEditType">
     <mediaobject>
        <imageobject>
           <imagedata fileref="./screenshots/manageType2.png" width="100%"/>
        </imageobject>
        <caption>
           <para>Edit Mode</para>
        </caption>
     </mediaobject>
  </para>
</section>

要:            截图

  <para xreflabel="New Type" xml:id="manageNewType">
     <mediaobject>
        <imageobject>
           <imagedata fileref="screenshots/manageType1.png" width="100%"/>
        </imageobject>
        <caption>
           <para>New Mode</para>
        </caption>
     </mediaobject>
  </para>

  <para xreflabel="Edit Type" xml:id="manageEditType">
     <mediaobject>
        <imageobject>
           <imagedata fileref="screenshots/manageType2.png" width="100%"/>
        </imageobject>
        <caption>
           <para>Edit Mode</para>
        </caption>
     </mediaobject>
  </para>
</section>

这是我当前的样式表,由于某种原因似乎没有做任何事......

<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="imagedata/@fileref[starts-with(.,'./')
    and not(starts-with(.,'./screenshots/'))
    ]">
    <xsl:attribute name="fileref">
        <xsl:value-of select="substring-after(substring-after(.,'./'), '/')"/>
    </xsl:attribute>
    </xsl:template>

    <xsl:template match="imagedata/@fileref[starts-with(.,'./screenshots/')]">
    <xsl:attribute name="fileref">
        <xsl:value-of select="substring-after(.,'./')"/>
    </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

修改在部分添加了xmlns =“http://docbook.org/ns/docbook”version =“5”。

1 个答案:

答案 0 :(得分:1)

此转化

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:d="http://docbook.org/ns/docbook">
   <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=
   "d:imagedata/@fileref
               [contains(., '/screenshots/')
              and
               not(starts-with(., 'screenshots/'))
               ]">
    <xsl:attribute name="fileref">
        <xsl:value-of select=
         "concat('screenshots/', substring-after(., '/screenshots/'))"/>
    </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>
在提供的XML文档上执行

<section xmlns="http://docbook.org/ns/docbook" version="5">
  <title>Screenshot</title>

  <para xreflabel="New Type" xml:id="manageNewType">
     <mediaobject>
        <imageobject>
           <imagedata fileref="./views/screenshots/manageType1.png" width="100%"/>
        </imageobject>
        <caption>
           <para>New Mode</para>
        </caption>
     </mediaobject>
  </para>

  <para xreflabel="Edit Type" xml:id="manageEditType">
     <mediaobject>
        <imageobject>
           <imagedata fileref="./screenshots/manageType2.png" width="100%"/>
        </imageobject>
        <caption>
           <para>Edit Mode</para>
        </caption>
     </mediaobject>
  </para>
</section>

生成想要的正确结果

<section xmlns="http://docbook.org/ns/docbook" version="5">
    <title>Screenshot</title>
    <para xreflabel="New Type" xml:id="manageNewType">
        <mediaobject>
            <imageobject>
                <imagedata fileref="screenshots/manageType1.png" width="100%"/>
            </imageobject>
            <caption>
                <para>New Mode</para>
            </caption>
        </mediaobject>
    </para>
    <para xreflabel="Edit Type" xml:id="manageEditType">
        <mediaobject>
            <imageobject>
                <imagedata fileref="screenshots/manageType2.png" width="100%"/>
            </imageobject>
            <caption>
                <para>Edit Mode</para>
            </caption>
        </mediaobject>
    </para>
</section>