简体   繁体   English

xsl替换字符串中的特定字符

[英]xsl replace specific character in string

I am studying XSL and I have question about replacing specific character in string. 我正在研究XSL,并且对替换字符串中的特定字符有疑问。

we have xml file 我们有xml文件

<family>
-<familyid id="first">
--<name>smith</name>
--<image>fatherpic\myfather.jpg</image>

and i would like get image path to insert picture. 我想获取图像路径来插入图片。

For example, we have path "fatherpic\\myfather.jpg" 例如,我们有路径“ fatherpic \\ myfather.jpg”

then i would like select "fatherpic/myfather.jpg" 那么我想选择“ fatherpic / myfather.jpg”

which mean i would like to change"/" to"\\". 这意味着我想将“ /”更改为“ \\”。

i was trying to use translate function. 我试图使用翻译功能。 but it didn't work. 但这没用。

does any can give example ? 有什么可以举例吗? thanks 谢谢

The following xslt will print replace the '\\' with '/' in the image element and will print the rest of the xml file unchanged. 以下xslt将在图像元素中打印将'\\'替换为'/',并将其余的xml文件保持不变。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" />
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="image">
        <image>
            <xsl:value-of select="translate(., '\', '/')" />
        </image>
    </xsl:template>
</xsl:stylesheet>

As you said in your post, you could use the translate function. 如您在帖子中所说,您可以使用翻译功能。

The following stylesheet extracts the value of the image value and outputs it like text after doing the described string translation. 下面的样式表提取图像值的值,并在完成描述的字符串转换后将其输出为文本。 This is just an example of how to use the translate function. 这只是如何使用翻译功能的示例。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="text" indent="no" />

    <xsl:template match="text()" />

    <xsl:template match="image">
        <xsl:value-of select="translate(., '\', '/')" />
    </xsl:template>

</xsl:stylesheet>

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM