문제

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.

도움이 되었습니까?

해결책

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top