XSL: إزالة المجلدات السابقة على ImageObject FileRef
سؤال
أريد أن أزيل مسبقًا "./" و المجلد الأول إذا لم يتم تسميته "لقطات شاشة" لأي فيليف ينتمي إلى Imagedata
لذلك من
<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" الإصدار = "5" إلى القسم.
المحلول
هذا التحول:
<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>
لا تنتمي إلى StackOverflow