使用 Translate 函数删除 xml 中的换行符,但如何忽略某些标签? [英] Using the Translate function to remove newline characters in xml, but how do I ignore certain tags?

查看:21
本文介绍了使用 Translate 函数删除 xml 中的换行符,但如何忽略某些标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照问题 - 从 a 中删除所有 \n\r 个字符node XSLT?看起来已经解决了,我正在使用这个解决方案,但偶然发现了这个场景-

Following the question- Removing all \n\r characters from a node XSLT? that looks to be solved, I am using this solution, but stumbled across this scenario-

如果我们不想在所有节点中替换换行符怎么办.例如,如果用户在网页上输入新行,则某些节点(如描述、说明)旨在存储新行.

What if we do not want the new line characters to be replaced in all the nodes. eg- Certain nodes like Description, Instructions are meant to store new lines if the user entered them on the webpage.

<T>
    <Name>Translate test</Name>
    <AlternateId>testid1</AlternateId>
    <Description>Translate test</Description>
    <Instructions>there is a new line between line1 and line2
    line1-asdfghjkl
    line2-asdfghjkl</Instructions>
    <Active>1</Active>
</T>

使用 translate(.,' ','') 后,现在的 xml 是这样的:

After using translate(.,' ',''), this is how the xml looks like now:

<T>
    <Name>Translate test</Name>
    <AlternateId>testid1</AlternateId>
    <Description>Translate test</Description>
    <Instructions>there is a new line between line1 and line2line1-asdfghjklline2-asdfghjkl</Instructions>
    <Active>1</Active>
</T>

我有超过 100 个这样的标签,我不想被翻译.有没有办法忽略这种不需要的标签的翻译?任何及时的帮助将不胜感激.

I have >100 such tags that I don't want to be translated. Is there a way to ignore the translation of such unwanted tags? Any timely help would be greatly appreciated.

问候,阿希什K

推荐答案

通常这样的任务是从一个模板开始来解决的,这个模板不改变地复制节点

Usually such a task is solved by starting with a template that copies nodes unchanged

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

现在您可以添加模板来转换您想要的元素,例如

Now you can add templates to transform the elements you want e.g.

<xsl:template match="Description | Instructions">
  <xsl:copy>
    <xsl:value-of select="translate(., '&#10;', '')"/>
  </xsl:copy>
</xsl:template>

当然,您需要在 match 属性中列出所有要转换的元素名称,但不必拼出不想转换的元素的名称.

Of course you would need to list all element names you want to be transformed in the match attribute but you don't have to spell out the names of elements you don't want to be transformed.

这篇关于使用 Translate 函数删除 xml 中的换行符,但如何忽略某些标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆