我想删除前面的'./'和第一个文件夹,如果它没有命名为'screenshots'
所以来自
<image href="./folderx/screenshots/page1.png">
<image href="./screenshots/page2.png"/>
<image href="./views/screenshots/table/screenshots/page3.png"/>
到
<image href="screenshots/page1.png">
<image href="screenshots/page2.png"/>
<image href="screenshots/table/screenshots/page3.png"/>
下面的解决方案是这样做的:
<?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/Type1.png" placement="break"/>
</p>
<p id="Type2">
<image href="./views/screenshots/Type2.png" placement="break"/>
</p>
</section>
</refbody>
</reference>
要:
<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>
使用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>
我不确定这是否相关,但我正在使用oXygen 12.0
答案 0 :(得分:1)
更新:OP(@Ace)修改了他的问题并报告下面的转换产生了一些新的附加属性。
这种效果是由于他正在使用<!DOCTYPE
而相关的DTD为某些元素提供了一些默认属性。
所以,没什么新鲜的或令人惊讶的! :)
当您有一条将DTD与文档相关联的<!DOCTYPE
指令以及此DTD具有默认属性时,将始终发生这种情况。
要删除属性,如果不需要,可以添加与空体匹配的状态。 但是,请注意,这可能会导致DTD的整个结果无效!
这是身份规则的机械覆盖:
<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>
将此转换应用于以下XML文档:
<t>
<image href="./folderx/screenshots/page1.png"/>
<image href="./screenshots/page2.png"/>
<image href="./views/screenshots/table/screenshots/page3.png"/>
</t>
生成了想要的结果:
<t>
<image href="screenshots/page1.png"></image>
<image href="screenshots/page2.png"></image>
<image href="screenshots/table/screenshots/page3.png"></image>
</t>