【问题标题】:XSL list of words to replace, most easy defintionXSL 要替换的单词列表,最简单的定义
【发布时间】:2012-12-06 00:01:49
【问题描述】:

我有一个想要复制的 XML 文档

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

【问题讨论】:

    标签: xml xslt xslt-2.0


    【解决方案1】:

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

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

    <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 函数也是可能的。

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

    <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>
    

    【讨论】:

    • 谢谢,我使用的是 v2,但 created 属性始终为空(在每个元素中,即使我只定义了一个测试短语)。测试对你有用吗?
    • 对不起,我的错。我在事物周围包裹了一个“选择”子句,以阻止非相关值被空字符串替换
    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 2011-01-08
    • 2022-12-10
    • 2012-05-06
    • 2021-12-14
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    相关资源
    最近更新 更多