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

发布于 2024-10-03 15:21:18 字数 2821 浏览 4 评论 0原文

我想删除前面的“./”第一个文件夹,如果它没有为属于 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" 到部分。

I want to remove preceding './' and first folder if it is not named 'screenshots' for any fileref that belongs to imagedata

So from

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

To:

Screenshot

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

Here is my current stylesheet that does not appear to be doing nothing for some reason...

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

Edit Added xmlns="http://docbook.org/ns/docbook" version="5" to section.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-10-10 15:21:18

此转换

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

This transformation:

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

when performed on the provided XML document:

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

produces the wanted, correct result:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文