【问题标题】:XSL: replace white space by hyphenXSL:用连字符替换空格
【发布时间】:2017-12-17 19:45:24
【问题描述】:

我正在尝试用“-”翻译空白。 数据来自TEI-XML数据:

<m type="base"> 
  <m type="baseForm">A<c type="infix">B</c>CD</m> 
 </m>

XSL 文件:

<?xml version="1.0" encoding="UTF-8"?>

  <xsl:for-each select="descendant::m[@type='base']/m[@type='baseForm']">  
    <xsl:choose>
      <xsl:when test="current()/c"><xsl:value-of select="current()[not != c[@type['infix']]] |node()"/></xsl:when>
     <xsl:otherwise><xsl:value-of select="current()"/></xsl:otherwise>
    </xsl:choose>
    <!-- my attempt -->
    <xsl:value-of select="translate(., ' ','-')"/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

在这篇帖子XSL replace space with caret的回答之后,我使用了translate,但它不起作用。 结果应该是:“A-B-CD”,但我有“A B CD”。

感谢您的热心帮助。

【问题讨论】:

  • translate 允许您将一个字符替换为另一个字符,但坦率地说,在示例&lt;m type="baseForm"&gt;A&lt;c type="infix"&gt;B&lt;/c&gt;CD&lt;/m&gt; 中我根本看不到任何空格,所以您想用translate(., ' ','-') 实现什么?第二个参数是空格?
  • 是的,我知道TEI-XML 中没有空格,但不幸的是,输出带有空格。我不知道为什么...
  • 您使用 ,其中 current() 是一个包含多个文本节点的“m”节点。默认情况下,xsl:value-of 使用空格作为分隔符(参见此处:w3.org/TR/xslt/#constructing-simple-content)。
  • 更一般的评论:我建议您将样式表组织为多个模板。为 c[@type='infix'] 定义一个特定模板,为您提供 -B- (在您的示例中)。让 xsl:apply-templates 发挥作用。
  • @Vanessa 问题是你在循环中使用 ,但不是整个输出 - 你需要将逻辑放入变量中,然后使用此变量执行翻译功能。请看我的回答。

标签: xml xslt tei


【解决方案1】:

据我所知,空格的问题将是 XML 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<m type="base"> 
  <m type="baseForm">
      A
      <c type="infix">
          B
      </c>
      CD
  </m> 
 </m>

在这种情况下,您的逻辑可以放在variable 中,然后执行translate 函数,请参见下面的XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text" />
    <xsl:template match="/">
        <!--put your logic in variable-->
        <xsl:variable name="str">
            <xsl:for-each select="descendant::m[@type='base']/m[@type='baseForm']">
                <xsl:value-of select="."/>
            </xsl:for-each>
        </xsl:variable>
    <!--normalize space will prevent spaces from left and right of string, then all spaces inside will be replaced by '-' -->
    <xsl:value-of select="translate(normalize-space($str), ' ', '-')"/>
 </xsl:template>
</xsl:stylesheet>

结果如预期:

A-B-CD

【讨论】:

  • 太棒了!我只需将select="." /&gt; 替换为select="current()[not != c[@type['infix']]] |node()"/&gt; 即可。谢谢@O.F.
【解决方案2】:

我们也可以用模板替换,只翻译一次:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="text()" priority="2">
    <xsl:value-of select="translate(., &quot; &quot;, &quot;,&quot;)" />
  </xsl:template>
  <xsl:template match="/">
        <xsl:element name="Your1stNode">
          <xsl:apply-templates select="Your1stNode/text()" />
        </xsl:element>
        <xsl:element name="Your2ndNode">
          <xsl:apply-templates select="Your2ndNode/text()" />
        </xsl:element>
   </xsl:template>
</xsl:stylesheet>

您可以在整个文档中使用 text() 将所有空格替换为逗号。

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2014-08-31
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多