tags:

views:

44

answers:

1

I want to remove preceding './' and first folder if it is not named 'screenshots'

So from

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

to

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

The Solution below does this:

<?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"&gt;
<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>

To:

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

Using XSLT:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <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>

I'm not sure if this is relevant, but I'm using oXygen 12.0

+1  A: 

Update: The OP (@Ace) has modified his question and reports that the transformation below produces some new, additional attributes.

This effect is due to the fact that he is using <!DOCTYPE and the associated DTD gives some default attributes to some of the elements.

So, nothing new or surprizing! :)

This will always happen when you have a <!DOCTYPE instruction that associates a DTD to the document and when this DTD has default attributes.

To remove the attributes, in case they are not wanted, one can add matching tempate(s) with empty body. However, be aware that this may render the whole result invalid for the DTD!


This is a mechanical override of the identity rule:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <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>

when this transformation is applied on the following XML document:

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

the wanted result is produced:

<t>
    <image href="screenshots/page1.png"></image>
    <image href="screenshots/page2.png"></image>
    <image href="screenshots/table/screenshots/page3.png"></image>
</t>
Dimitre Novatchev
Yes Perfect! The parsing works exactly the way i want it to. Just of out curiosity, when I run this on my .dita file, i get class="- topic/p "> added to all my <p> tags, and I have similar additions to all my other tags. Is there an option i should be setting?
Ace
@Ace: It isn't possible that this transformation adds a new attribute -- I'd be interested to see your XML document.
Dimitre Novatchev
@Dimitre: I agree your .xsl does not have anything to suggest that it would do that. I've posted whats happening as an edit to my answer.
Ace
@Ace: I don't understand your latest edits. ???
Dimitre Novatchev
@Dimitre: My Answer has been edited to show the xml before and after I apply your transformation.
Ace
@Ace: just the XML is not sufficient. Please, provide the minimal transformation (XSLT stylesheet) that produces the reported results from the provided XML document.
Dimitre Novatchev
@Dimitre: The XSLT is identical to the one you have supplied. I've added to my question.
Ace
@Ace: This is due to the `<!DOCTYPE` instruction. Obviously, the DTD contains some default attributes. If you don't want these, simply remove the `<!DOCTYPE` instruction. I have verified that with it removed, you don't get the additional attributes.
Dimitre Novatchev
@Ace: I added an Update to my answer, explaining your problem.
Dimitre Novatchev