0

我需要<mo>根据其中的字符转换 XML 元素。例如,如果元素只包含大写字符,我需要转换为“obar”。但是当它只包含小写字符时,我需要转换为“orule”。如果元素同时包含大写和小写字符,那么我需要转换为“obar”。此外,如果元素包含 XSLT 代码中给出的任何升序字符,它应该是“obar”。

我已经使用了<xsl:if>条件。我只需要使用一次<xsl:if>条件

我只需要通过 XSLT 1.0 进行转换。

示例 XML 代码:

<?xml version="1.0"?><chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <p>
    <math display="block">
      <mrow>
        <mover accent='true'>
          <mrow>
            <mi>R</mi>
            <mi>T</mi>
          </mrow>
          <mo stretchy='true'>&#x00AF;</mo>
        </mover>
        <mover accent='true'>
          <mrow>
            <mi>a</mi>
            <mi>A</mi>
          </mrow>
          <mo stretchy='true'>&#x00AF;</mo>
        </mover>
        <mover accent='true'>
          <mrow>
            <mi>a</mi>
            <mi>a</mi>
          </mrow>
          <mo stretchy='true'>&#x00AF;</mo>
        </mover>
        <mover accent='true'>
          <mrow>
            <mi>a</mi>
            <mi>h</mi>
          </mrow>
          <mo stretchy='true'>&#x00AF;</mo>
        </mover>
      </mrow>
    </math>
  </p>
</chapter>

XSLT 1.0 试过:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:m="http://www.w3.org/1998/Math/MathML" >
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:mover[@accent='true']">
<xsl:choose>
<xsl:when test="(self::*/child::*[2]/self::m:mo[@stretchy='true']/text())='¯'">
<xsl:if test="(self::*/child::*[1]/self::m:mrow/child::*/text())='b' or 'd' or 'f' or 'h' or 'i' or 'j' or 'k' or 'l' or 'r' or 't' or 'A' or 'B' or 'C' or 'D' or 'E' or 'F' or 'G' or 'H' or 'I' or 'J' or 'K' or 'L' or 'M' or 'N' or 'O' or 'P' or 'Q' or 'R' or 'S' or 'T' or 'U' or 'V' or 'W' or 'X' or 'Y' or 'Z'"><xsl:text>*obar*{</xsl:text></xsl:if>
<xsl:if test="(self::*/child::*[1]/self::m:mrow/child::*/text())='a' or 'c' or 'e' or 'g' or 'm' or 'n' or 'o' or 'p' or 'q' or 's' or 'u' or 'v' or 'w' or 'x' or 'y' or 'z'"><xsl:text>*orule*{</xsl:text></xsl:if>
<xsl:apply-templates/>}</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="m:mrow"><xsl:apply-templates/></xsl:template>
<xsl:template match="m:mi"><xsl:apply-templates/></xsl:template>
<xsl:template match="m:mo"></xsl:template>
</xsl:stylesheet>

需要输出:

<chapter xmlns="http://www.w3.org/1998/Math/MathML">
  <p>
    <math display="block">*obar*{RT}*obar*{aA}*orule*{aa}*obar*{ah}</math>
  </p>
</chapter>
4

1 回答 1

0

这个怎么样:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1998/Math/MathML" xmlns:m="http://www.w3.org/1998/Math/MathML" >
  <xsl:output method="xml" encoding="UTF-8" indent="no"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="m:mover[@accent='true']">
    <xsl:if test="*[2][@stretchy='true'] = '&#x00AF;'">
      <xsl:choose>
        <!-- translate(value, 'listOfCharacters', '') != .  will be true if value contains any of the characters in the list-->
        <xsl:when test="m:mrow/*[translate(., 'bdfhijklrtABCDEFGHIJKLMNOPQRSTUVWXYZ', '') != .]">
          <xsl:text>*obar*</xsl:text>
        </xsl:when>
        <xsl:otherwise>*orule*</xsl:otherwise>
      </xsl:choose>
      <xsl:text>{</xsl:text>
      <xsl:apply-templates/>
      <xsl:text>}</xsl:text>
    </xsl:if>
  </xsl:template>
  <xsl:template match="m:mrow | m:mi">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="m:mo" />
</xsl:stylesheet>
于 2013-01-12T05:55:43.633 回答