要替换的 XSL 单词列表,最简单的定义 [英] XSL list of words to replace, most easy defintion

查看:27
本文介绍了要替换的 XSL 单词列表,最简单的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要处理的 XML 文档<,但我不知道如何翻译一个特殊属性.复制 XML 并替换 gernal 中的属性工作正常,但我不知道如何在 XSL 中定义一个短语列表,然后将其翻译成另一个短语.

I've got a XML document that I want to cop<, but I don't know how to translate one special attribute. Copying the XML and replacing the attribute in gernal works fine, but I don't know how I can define a list of phrases in XSL that are then translated into another phrase.

定义应该易于阅读.translate() 是否吞下了某种列表表示?一个使用 translate 的小例子会很棒(不要关心 XML 复制的东西).

The definition should be easyly readable. Does translate() swallow some kind of list representation? A small example using translate would be great (don't care about the XML copying stuff).

推荐答案

XPath 和 XSLT 1.0 的 translate 功能仅用于将一个 Unicode 字符替换为另一个 Unicode 字符;您可以提供一个输入和替换字符列表,然后第一个列表中的每个字符将被第二个列表中相同位置的字符替换.但是要替换完整的作品或短语,您需要其他工具.

The translate function of XPath and XSLT 1.0 serves only to replace one Unicode character by another Unicode character; you can provide a list of input and replacement characters where then each character in the first list is then replaced by the one in the second list at the same position. But to replace complete works or phrases you need other tools.

您尚未说明或描述是否要替换完整的属性值,假设您可以(使用 XSLT 2.0)简单地执行例如

You have not said or described whether you want to replace the complete attribute value, assuming that you can (with XSLT 2.0) simply do e.g.

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

<xsl:key name="phrase" match="phrase" use="@input"/>

<xsl:param name="phrases">
  <phrases>
    <phrase input="IANAL" output="I am not a lawyer"/>
    <phrase input="WYSIWYG" output="What you see is what you get"/>
  </phrases>
</xsl:param>

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


<xsl:template match="foo/@bar">
  <xsl:attribute name="baz" select="key('phrase', ., $phrases)/@output"/>
</xsl:template>

</xsl:stylesheet>

例如那个样式表转换

<root>
  <foo bar="IANAL"/>
  <foo bar="WYSIWYG"/>
</root>

进入

<root>
  <foo baz="I am not a lawyer"/>
  <foo baz="What you see is what you get"/>
</root>

如果您想对一个值或字符串中的子字符串进行多次替换,则需要付出更多努力,但使用 XSLT/XPath 2.0 中的 replace 函数也是可能的.

If you want to do several replacements of substrings in one value or string then more effort is needed but with the replace function in XSLT/XPath 2.0 that is possible too.

这是一个使用项目列表和递归函数替换短语的示例:

[edit]Here is an example using a list of items and a recursive function replacing phrases:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:mf="http://example.com/mf"
  exclude-result-prefixes="xs mf"
  version="2.0">

<xsl:key name="phrase" match="phrase" use="@input"/>

<xsl:function name="mf:replace-phrases" as="xs:string">
  <xsl:param name="phrases" as="element(phrase)*"/>
  <xsl:param name="text" as="xs:string"/>
  <xsl:choose>
    <xsl:when test="not($phrases)">
      <xsl:sequence select="$text"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:sequence select="mf:replace-phrases($phrases[position() gt 1], replace($text, $phrases[1]/@input, $phrases[1]/@output))"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

<xsl:param name="phrases">
  <phrases>
    <phrase input="IANAL" output="I am not a lawyer"/>
    <phrase input="WYSIWYG" output="What you see is what you get"/>
  </phrases>
</xsl:param>

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


<xsl:template match="foo/@bar">
  <xsl:attribute name="baz" select="mf:replace-phrases($phrases/phrases/phrase, .)"/>
</xsl:template>

</xsl:stylesheet>

改变例子

<root>
  <foo bar="He said: 'IANAL'. I responded: 'WYSIWYG', and he smiled."/>
</root>

进入

<root>
  <foo baz="He said: 'I am not a lawyer'. I responded: 'What you see is what you get', and he smiled."/>
</root>

这篇关于要替换的 XSL 单词列表,最简单的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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