仅通过 XSLT 从 XML 中删除特定标签的特殊字符

发布于 2024-09-08 08:45:13 字数 521 浏览 3 评论 0原文

我的 XML 中存在特殊字符的问题。 基本上,我使用 Xalan Processor 将一个 xml 拆分为多个 xml。

当分割文档时,我使用它们的名称标签的值作为生成的文件的名称。问题在于该名称包含 XML 处理器无法识别的字符,例如 ™ (TM) 和 ® (R)。我只想在命名文件时删除这些字符。

<xsl:template match="products">
    <redirect:write select="concat('..\\xml\\product\\en\\',translate(string(name),'&lt;/&gt; ',''),'.xml')">

上面是我编写的用于将 XML 拆分为多个 XML 的 XSL 代码。正如你所看到的,我正在使用 hte 翻译方法来替换 '/','<','>'名称中带有“”。我希望我可以对 ™ (TM) 和 ® (R) 做同样的事情,但它似乎不起作用。 请告诉我如何才能做到这一点。

感谢您提前提供帮助。

I am having a certian issue with special characters in my XML.
Bascially I am splitting up an xml into multiple xmls using Xalan Processor.

When splitting the documents up I am using their value of the name tag as the name of the file generated. The problem is that the name contains characters that arent recognized by the XML processor like ™ (TM) and ® (R). I want to remove those characters ONLY when naming the files.

<xsl:template match="products">
    <redirect:write select="concat('..\\xml\\product\\en\\',translate(string(name),'</> ',''),'.xml')">

The above is the XSL code I have writter to split the XML into multlpe XMLs. As you can see I am using hte translate method to subtitute '/','<','>' with '' from the name. I was hoping I could do the same with ™ (TM) and ® (R) but it doesnt seem to work.
Please advice me how I would be able to do that.

Thanks for you help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

絕版丫頭 2024-09-15 08:45:13

我没有 Xalan,但使用 8 个其他 XSLT 处理器,此转换:

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

 <xsl:template match="text()">
   <xsl:value-of select="translate(., '</>™®', '')"/>
   ===================
   <xsl:value-of select="translate(., '</>™®', '')"/>
 </xsl:template>
</xsl:stylesheet>

应用于此 XML 文档时:

<t>XXX™ My Trademark®</t>

产生所需的结果:

XXX My Trademark
   ===================
   XXX My Trademark

< strong>我建议您尝试使用上面两个表达式之一——至少第二个可能会成功。

I don't have Xalan, but with 8 other XSLT processors this thransformation:

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

 <xsl:template match="text()">
   <xsl:value-of select="translate(., '</>™®', '')"/>
   ===================
   <xsl:value-of select="translate(., '</>™®', '')"/>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>XXX™ My Trademark®</t>

produces the wanted result:

XXX My Trademark
   ===================
   XXX My Trademark

I suggest that you try to use one of the two expressions above -- at least the second may work successfully.

依 靠 2024-09-15 08:45:13

根据 Dimitre 的回答,我认为如果您不确定 name 中可能包含哪些特殊字符,也许您应该保留您认为的法律文档名称字符。

例如:

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

 <xsl:template match="text()">
   <xsl:value-of select="translate(.,
                                   translate(.,
                                             'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ',
                                             ''),
                                   '')"/>
 </xsl:template>
</xsl:stylesheet> 

输入:

<t>XXX™ My > Trademark®</t>

结果:

XXX My  Trademark

Following Dimitre answer, I think that if you are not sure about wich special character could be in name, maybe you should keep what you consider legal document's name characters.

As example:

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

 <xsl:template match="text()">
   <xsl:value-of select="translate(.,
                                   translate(.,
                                             'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ',
                                             ''),
                                   '')"/>
 </xsl:template>
</xsl:stylesheet> 

With input:

<t>XXX™ My > Trademark®</t>

Result:

XXX My  Trademark
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文