XSL: removing preceding folders on image path
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">
<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/T开发者_StackOverflow社区ype1.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">
<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
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">
<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>
精彩评论