XSLT:更改路径字符串的一部分
我是XSLT1.0新手。在XML中的string元素中,我想将jar文件(C:\abc\my.jar)路径的目录部分更改为静态字符串,并保留jar文件名($path\u VAR$\my.jar) 原始XML代码段如下所示:XSLT:更改路径字符串的一部分,xslt,Xslt,我是XSLT1.0新手。在XML中的string元素中,我想将jar文件(C:\abc\my.jar)路径的目录部分更改为静态字符串,并保留jar文件名($path\u VAR$\my.jar) 原始XML代码段如下所示: <object class="someclass"> <property name="path"> <string><![CDATA[C:\abc\my.jar]]></string> <
<object class="someclass">
<property name="path">
<string><![CDATA[C:\abc\my.jar]]></string>
</property>
</object>
我希望转换后的XML是:
<object class="someclass">
<property name="path">
<string><![CDATA[$PATH_VAR$\my.jar]]></string>
</property>
</object>
请注意,原始路径可以是任意长度(\\xyz\abc或Z:\abc\def\ghi),jar文件可以命名为任何名称
我不知道如何解析原始路径字符串并仅更改其中的目录部分。请帮忙
-Jeff在XSLT 2.0中,只提取文件名(即最后一个
\
分隔符之后的部分)很容易:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" cdata-section-elements="string"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="string/text()">
<xsl:text>$PATH_VAR$\</xsl:text>
<xsl:value-of select="tokenize(., '\\')[last()]"/>
</xsl:template>
</xsl:stylesheet>
$PATH_VAR$\
这是XSLT1.0。但是看起来您正在使用XSLT2.0,所以它没有用=)
“仅更改父路径零件”父零件不是第一个“\”
之前的零件吗?这将使您的结果“$PATH\u VAR$\abc\my.jar”
,不是吗?--另外,请注明XSLT 1.0或2.0。我指的是最高“\my.jar”。我目前正在使用XSLT 2.0。我删除了对父路径的引用,希望避免混淆。非常感谢您的快速回答!然而,我刚刚读了另一篇文章,发现我真的在使用XLST1.0。很抱歉。@JeffM哪个特定的XSLT 1.0处理器?我的xsl文件中的样式表行:。这就是你的意思吗?不,我是说哪个XSLT引擎运行你的样式表。请看这里如何找到答案:“我目前正在使用XSLT 2.0‘v.s.”我刚刚读了另一篇文章,发现我真的在使用XLST 1.0’-我的头现在要爆炸了:)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" cdata-section-elements="string"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="string/text()">
<xsl:choose>
<xsl:when test="substring-after(., '\')">
<xsl:call-template name="process">
<xsl:with-param name="left" select="."/>
</xsl:call-template>
</xsl:when>
<!-- no slash == no $PATH_VAR$ -->
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="process">
<xsl:param name="left"/>
<xsl:choose>
<!-- if we have at least one slash ahead - cut everything before first slash and call template recursively -->
<xsl:when test="substring-after($left, '\')">
<xsl:call-template name="process">
<xsl:with-param name="left" select="substring-after($left, '\')"/>
</xsl:call-template>
</xsl:when>
<!-- if there are no slashes ahead then we have only file name left, that's all -->
<xsl:otherwise>
<xsl:value-of select="concat('$PATH_VAR$\', $left)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>