xsl

 
转换步骤:
  第一步:创建模板
处理元素列表
 ① <xsl: template  ></xsl: template >::为输出定义一个处理规则
       属性: match:声明范围
 ②<xsl: value-of>;  插入被选择节点的知道输出文档
       属性:select=“被选择的元素名”
 ③<xsl:apply-templates></…>;指导XSL处理器选择一个基于某个匹配的模板;
④<xsl:for-cach></….>:对select属性指定的节点集合中的子节点循环执行处理;
⑤<xsl:attribute></…>:在新增文件的元素中,动态增加属性
⑥<xsl:choose>:类似switch
   <xsl:when>:类似case(满足一个具体条件)
   <xsl:otherwise>:类似default
⑦<xsl:if>:简单条件判断
 。。。。
 
第二步:
编写html文件来规定数据的显示样式
保存
  在***.xsl点击:上面有个按钮“XSL转换” 选择被转换的xml文件—确定
 
第三部分:php应用程序使用xml
1)         SimpleXML:语法简单、功能少;
2)         DOM(父节点、子节点、叶节点)
 
注意:*.html、*.xml、*.ent、*.dtd、*.xsl、*.xslt等文档的显示都依赖浏览器的解析,
      存放位置无所谓,与服务器环境无关;
 
 
第一种:SIMPLEXML
www目录下:
1、             php创建xml文档,fopen
2、             $str1 =<<<XML             任意指定开始标记,该标记后不能有空格
……内容…..
XML;                    结束标记要和开始标记保持一致,该标记前不能有空格
创建xml文档---内置类SimpleXMLElement(string  var);
反射:(预留)
 
例如:
  
<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
</movie>
</movies>
XML;
//利用内置类SimpleXMLElement创建对象
//new 构造方法(要转换成xml文档的字符串变量当作实参)
//
$_sx = new SimpleXMLElement($xmlstr);
//调用该对象的asXML方法进行xml文档的生成
//asXML("转换成功之后xml文档的名字");
$_sx->asXML("simple.xml");
 

?>
 
 
 
 

 

这个也是公式的xsl<?xml version='1.0' encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" version='1.0'> <!-- ====================================================================== --> <!-- $Id: tokens.xsl,v 1.7 2003/06/10 12:24:05 shade33 Exp $ This file is part of the XSLT MathML Library distribution. See ./README or http://www.raleigh.ru/MathML/mmltex for copyright and other information --> <!-- ====================================================================== --> <xsl:template match="m:mi|m:mn|m:mo|m:mtext|m:ms"> <xsl:call-template name="CommonTokenAtr"/> </xsl:template> <!-- 3.2.9 mglyph --> <xsl:template match="m:mglyph"> <xsl:text>\textcolor{red}{</xsl:text> <xsl:value-of select="@alt"/> <xsl:text>}</xsl:text> </xsl:template> <xsl:template name="mi"> <xsl:choose> <xsl:when test="string-length(normalize-space(.))>1 and not(@mathvariant)"> <xsl:text>\mathrm{</xsl:text> <xsl:apply-templates/> <xsl:text>}</xsl:text> </xsl:when> <xsl:otherwise> <xsl:apply-templates/> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="mn"> <xsl:choose> <xsl:when test="string(number(.))='NaN' and not(@mathvariant)"> <xsl:text>\mathrm{</xsl:text> <xsl:apply-templates/> <xsl:text>}</xsl:text> </xsl:when> <xsl:otherwise> <xsl:apply-templates/> </xsl:otherwise> </xsl:choose> </xsl:template> <!-- 3.2.5 Math Operator --> <xsl:template name="mo"> <xsl:if test="translate(normalize-space(.),'()[]}|','{{{{{{')='{'"> <xsl:choose> <xsl:when test="not(@stretchy='false') and count(preceding-sibling::m:mo[translate(normalize-space(.),'()[]}|','{{{{{{')='{'])mod 2=0 and following-sibling::m:mo[1][not(@stretchy='false')][translate(normalize-space(.),'()[]}|','{{{{{{')='{']"> <xsl:text>\left</xsl:text> </xsl:when> <xsl:when test="not(@stretchy='false') and count(preceding-sibling::m:mo[translate(normalize-space(.),'()[]}|','{{{{{{')='{'])mod 2=1 and preceding-sibling::m:mo[1][not(@stretchy='false')][translate(normalize-space(.),'()[]}|','{{{{{{')='{']"> <xsl:text>\right</xsl:text> </xsl:when> </xsl:choose> </xsl:if> <xsl:apply-templates/> </xsl:template> <xsl:template name="mtext"> <xsl:variable name="content"> <xsl:call-template name="replaceMtextEntities"> <xsl:with-param name="content" select="normalize-space(.)"/> </xsl:call-template> </xsl:variable> <xsl:text>\text{</xsl:text> <xsl:value-of select="$content"/> <xsl:text>}</xsl:text> </xsl:template> <xsl:template match="m:mspace"> <xsl:text>\phantom{\rule</xsl:text> <xsl:if test="@depth"> <xsl:text>[-</xsl:text> <xsl:value-of select="@depth"/> <xsl:text>]</xsl:text> </xsl:if> <xsl:text>{</xsl:text> <xsl:if test="not(@width)"> <xsl:text>0ex</xsl:text> </xsl:if> <xsl:value-of select="@width"/> <xsl:text>}{</xsl:text> <xsl:if test="not(@height)"> <xsl:text>0ex</xsl:text> </xsl:if> <xsl:value-of select="@height"/> <xsl:text>}}</xsl:text> </xsl:template> <xsl:template name="ms"> <xsl:choose> <xsl:when test="@lquote"><xsl:value-of select="@lquote"/></xsl:when> <xsl:otherwise><xsl:text>''</xsl:text></xsl:otherwise> </xsl:choose><xsl:apply-templates/><xsl:choose> <xsl:when test="@rquote"><xsl:value-of select="@rquote"/></xsl:when> <xsl:otherwise><xsl:text>''</xsl:text></xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="CommonTokenAtr"> <xsl:if test="@mathbackground"> <xsl:text>\colorbox[rgb]{</xsl:text> <xsl:call-template name="color"> <xsl:with-param name="color" select="@mathbackground"/> </xsl:call-template> <xsl:text>}{$</xsl:text> </xsl:if> <xsl:if test="@color[not(@mathcolor)] or @mathcolor"> <!-- Note: @color is deprecated in MathML 2.0 --> <xsl:text>\textcolor[rgb]{</xsl:text> <xsl:call-template name="color"> <xsl:with-param name="color" select="@color|@mathcolor"/> </xsl:call-template> <xsl:text>}{</xsl:text> </xsl:if> <xsl:if test="@mathvariant"> <xsl:choose> <xsl:when test="@mathvariant='normal'"> <xsl:text>\mathrm{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='bold'"> <xsl:text>\mathbf{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='italic'"> <xsl:text>\mathit{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='bold-italic'"> <!-- not supported --> <xsl:text>\mathit{</xsl:text> <xsl:message>The value bold-italic for mathvariant is not supported</xsl:message> </xsl:when> <xsl:when test="@mathvariant='double-struck'"> <!-- Required amsfonts --> <xsl:text>\mathbb{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='bold-fraktur'"> <!-- not supported --> <xsl:text>\mathfrak{</xsl:text> <xsl:message>The value bold-fraktur for mathvariant is not supported</xsl:message> </xsl:when> <xsl:when test="@mathvariant='script'"> <xsl:text>\mathcal{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='bold-script'"> <!-- not supported --> <xsl:text>\mathcal{</xsl:text> <xsl:message>The value bold-script for mathvariant is not supported</xsl:message> </xsl:when> <xsl:when test="@mathvariant='fraktur'"> <!-- Required amsfonts --> <xsl:text>\mathfrak{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='sans-serif'"> <xsl:text>\mathsf{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='bold-sans-serif'"> <!-- not supported --> <xsl:text>\mathsf{</xsl:text> <xsl:message>The value bold-sans-serif for mathvariant is not supported</xsl:message> </xsl:when> <xsl:when test="@mathvariant='sans-serif-italic'"> <!-- not supported --> <xsl:text>\mathsf{</xsl:text> <xsl:message>The value sans-serif-italic for mathvariant is not supported</xsl:message> </xsl:when> <xsl:when test="@mathvariant='sans-serif-bold-italic'"> <!-- not supported --> <xsl:text>\mathsf{</xsl:text> <xsl:message>The value sans-serif-bold-italic for mathvariant is not supported</xsl:message> </xsl:when> <xsl:when test="@mathvariant='monospace'"> <xsl:text>\mathtt{</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>{</xsl:text> <xsl:message>Error at mathvariant attribute</xsl:message> </xsl:otherwise> </xsl:choose> </xsl:if> <xsl:call-template name="selectTemplate"/> <xsl:if test="@mathvariant"> <xsl:text>}</xsl:text> </xsl:if> <xsl:if test="@color or @mathcolor"> <xsl:text>}</xsl:text> </xsl:if> <xsl:if test="@mathbackground"> <xsl:text>$}</xsl:text> </xsl:if> </xsl:template> <xsl:template name="selectTemplate"> <xsl:choose> <xsl:when test="local-name(.)='mi'"> <xsl:call-template name="mi"/> </xsl:when> <xsl:when test="local-name(.)='mn'"> <xsl:call-template name="mn"/> </xsl:when> <xsl:when test="local-name(.)='mo'"> <xsl:call-template name="mo"/> </xsl:when> <xsl:when test="local-name(.)='mtext'"> <xsl:call-template name="mtext"/> </xsl:when> <xsl:when test="local-name(.)='ms'"> <xsl:call-template name="ms"/> </xsl:when> </xsl:choose> </xsl:template> <xsl:template name="color"> <!-- NB: Variables colora and valueColor{n} only for Sablotron --> <xsl:param name="color"/> <xsl:variable name="colora" select="translate($color,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')"/> <xsl:choose> <xsl:when test="starts-with($colora,'#') and string-length($colora)=4"> <xsl:variable name="valueColor"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,2,1)"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$valueColor div 15"/><xsl:text>,</xsl:text> <xsl:variable name="valueColor1"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,3,1)"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$valueColor1 div 15"/><xsl:text>,</xsl:text> <xsl:variable name="valueColor2"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,4,1)"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="$valueColor2 div 15"/> </xsl:when> <xsl:when test="starts-with($colora,'#') and string-length($colora)=7"> <xsl:variable name="valueColor1"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,2,1)"/> </xsl:call-template> </xsl:variable> <xsl:variable name="valueColor2"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,3,1)"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="($valueColor1*16 + $valueColor2) div 255"/><xsl:text>,</xsl:text> <xsl:variable name="valueColor1a"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,4,1)"/> </xsl:call-template> </xsl:variable> <xsl:variable name="valueColor2a"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,5,1)"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="($valueColor1a*16 + $valueColor2a) div 255"/><xsl:text>,</xsl:text> <xsl:variable name="valueColor1b"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,6,1)"/> </xsl:call-template> </xsl:variable> <xsl:variable name="valueColor2b"> <xsl:call-template name="Hex2Decimal"> <xsl:with-param name="arg" select="substring($colora,7,1)"/> </xsl:call-template> </xsl:variable> <xsl:value-of select="($valueColor1b*16 + $valueColor2b) div 255"/> </xsl:when> <!-- ======================= if color specifed as an html-color-name ========================================== --> <xsl:when test="$colora='aqua'"><xsl:text>0,1,1</xsl:text></xsl:when> <xsl:when test="$colora='black'"><xsl:text>0,0,0</xsl:text></xsl:when> <xsl:when test="$colora='blue'"><xsl:text>0,0,1</xsl:text></xsl:when> <xsl:when test="$colora='fuchsia'"><xsl:text>1,0,1</xsl:text></xsl:when> <xsl:when test="$colora='gray'"><xsl:text>.5,.5,.5</xsl:text></xsl:when> <xsl:when test="$colora='green'"><xsl:text>0,.5,0</xsl:text></xsl:when> <xsl:when test="$colora='lime'"><xsl:text>0,1,0</xsl:text></xsl:when> <xsl:when test="$colora='maroon'"><xsl:text>.5,0,0</xsl:text></xsl:when> <xsl:when test="$colora='navy'"><xsl:text>0,0,.5</xsl:text></xsl:when> <xsl:when test="$colora='olive'"><xsl:text>.5,.5,0</xsl:text></xsl:when> <xsl:when test="$colora='purple'"><xsl:text>.5,0,.5</xsl:text></xsl:when> <xsl:when test="$colora='red'"><xsl:text>1,0,0</xsl:text></xsl:when> <xsl:when test="$colora='silver'"><xsl:text>.75,.75,.75</xsl:text></xsl:when> <xsl:when test="$colora='teal'"><xsl:text>0,.5,.5</xsl:text></xsl:when> <xsl:when test="$colora='white'"><xsl:text>1,1,1</xsl:text></xsl:when> <xsl:when test="$colora='yellow'"><xsl:text>1,1,0</xsl:text></xsl:when> <xsl:otherwise> <xsl:message>Exception at color template</xsl:message> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="Hex2Decimal"> <xsl:param name="arg"/> <xsl:choose> <xsl:when test="$arg='f'"> <xsl:value-of select="15"/> </xsl:when> <xsl:when test="$arg='e'"> <xsl:value-of select="14"/> </xsl:when> <xsl:when test="$arg='d'"> <xsl:value-of select="13"/> </xsl:when> <xsl:when test="$arg='c'"> <xsl:value-of select="12"/> </xsl:when> <xsl:when test="$arg='b'"> <xsl:value-of select="11"/> </xsl:when> <xsl:when test="$arg='a'"> <xsl:value-of select="10"/> </xsl:when> <xsl:when test="translate($arg, '0123456789', '9999999999')='9'"> <!-- if $arg is number --> <xsl:value-of select="$arg"/> </xsl:when> <xsl:otherwise> <xsl:message>Exception at Hex2Decimal template</xsl:message> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template match="m:*/text()"> <xsl:call-template name="replaceEntities"> <xsl:with-param name="content" select="normalize-space()"/> </xsl:call-template> </xsl:template> </xsl:stylesheet>
10-21
你提供的这份 XSLT 是一个 **将 MathML 转换为 LaTeX** 的样式表(来自 [XSLT MathML Library](http://www.raleigh.ru/MathML/mmltex)),与前一份“OMML → MathML”的 XSLT 正好是**下游处理环节**。 --- ### ✅ 当前文件功能解析 | 文件 | 作用 | |------|------| | 第一个 XSLT (`omml2mml.xsl`) | 将 Word 的 OMML 公式转为标准 MathML | | 这个 XSLT (`tokens.xsl`) | 将 MathML 渲染成 **LaTeX 字符串**,用于排版输出 | 它主要处理: - `<mi>` → `\mathrm{}` 或普通变量 - `<mn>` → 数字 - `<mo>` → 操作符(自动加 `\left`/`\right`) - `<mtext>` → `\text{...}` - 颜色、字体、空格等格式转换 --- ## 🔧 常见修改需求及解决方案 你现在可能希望: > “在最终生成的 LaTeX 中,让 `dx dy dθ` 显示正确,不粘连,并且 `d` 是正体微分算子” 这是典型的数学排版问题。我们来一步步解决。 --- ### ✅ 目标:将 `dx` 输出为 `\mathrm{d}x\,` 即: - `d` 使用正体(`\mathrm{d}`) - 自动添加薄空格 `\,` - 多个微分如 `dx dy dθ` 不粘连 --- ### ✅ 修改方案:增强 `<mo>` 和 `<mi>` 的处理逻辑 我们需要在 `tokens.xsl` 中对形如 `d<x>` 的情况特殊处理。 #### ✅ 修改点:重写 `mi` 模板以识别微分符号 ```xml <xsl:template name="mi"> <xsl:variable name="content" select="normalize-space(.)" /> <!-- 特殊处理:如果内容是单个字母 'd' 并且后面紧跟着另一个变量(由上下文判断) --> <!-- 但由于 XSLT 1.0 很难看兄弟节点,建议用后处理或配合上层标记 --> <xsl:choose> <!-- 如果是 "d" 且父元素是微分上下文(比如被标记了 class 或 role) --> <xsl:when test="$content = 'd' and ancestor-or-self::*[@class='diff']"> <xsl:text>\mathrm{d}</xsl:text> </xsl:when> <!-- 多字符标识符默认正体 --> <xsl:when test="string-length($content) > 1 and not(@mathvariant)"> <xsl:text>\mathrm{</xsl:text> <xsl:apply-templates/> <xsl:text>}</xsl:text> </xsl:when> <!-- 其他情况原样输出 --> <xsl:otherwise> <xsl:apply-templates/> </xsl:otherwise> </xsl:choose> </xsl:template> ``` 但这依赖你在 MathML 上加自定义属性(如 `<mi class="diff">d</mi>`),否则无法区分普通 `d` 和微分 `d`。 --- ### ✅ 更佳实践:预处理 MathML 标记微分项 在调用此 XSLT 前,先用 Java 或其他工具把 `d<x>` 改写为: ```xml <mo mathvariant="normal">d</mo><mi>x</mi> ``` 然后在此 XSLT 中确保 `<mo>` 的 `mathvariant="normal"` 被正确处理。 #### ✅ 修改 `CommonTokenAtr` 模板支持 `mathvariant="normal"` on `<mo>` 当前代码只对 `<mi>`、`<mn>` 处理 `mathvariant`,但没考虑 `<mo>` 是否有该属性。 #### ✅ 补丁:扩展 `CommonTokenAtr` 对 `mo` 的 `mathvariant` 支持 ```xml <xsl:template name="CommonTokenAtr"> <xsl:if test="@mathbackground"> <xsl:text>\colorbox[rgb]{</xsl:text> <xsl:call-template name="color"> <xsl:with-param name="color" select="@mathbackground"/> </xsl:call-template> <xsl:text>}{$</xsl:text> </xsl:if> <xsl:if test="@color or @mathcolor"> <xsl:text>\textcolor[rgb]{</xsl:text> <xsl:call-template name="color"> <xsl:with-param name="color" select="@color|@mathcolor"/> </xsl:call-template> <xsl:text>}{</xsl:text> </xsl:if> <!-- 新增:支持 mo 的 mathvariant --> <xsl:if test="@mathvariant and local-name()='mo'"> <xsl:choose> <xsl:when test="@mathvariant='normal'"> <xsl:text>\mathrm{</xsl:text> </xsl:when> <xsl:when test="@mathvariant='bold'"> <xsl:text>\mathbf{</xsl:text> </xsl:when> <xsl:otherwise> <xsl:text>{</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:if> <!-- 原有 mathvariant 处理(mi/mn 等)保持不变 --> <xsl:if test="@mathvariant and local-name()!='mo'"> <!-- ... 原来的 choose 分支 ... --> </xsl:if> <xsl:call-template name="selectTemplate"/> <!-- 结束括号 --> <xsl:if test="@mathvariant and (local-name()='mo' or local-name()!='mo')"> <xsl:text>}</xsl:text> </xsl:if> <xsl:if test="@color or @mathcolor"> <xsl:text>}</xsl:text> </xsl:if> <xsl:if test="@mathbackground"> <xsl:text>$}</xsl:text> </xsl:if> </xsl:template> ``` --- ### ✅ 最终推荐流程(Java 实现) 由于 XSLT 1.0 缺乏上下文感知能力,最佳做法是在 **Java 层预处理 MathML**: ```java public String preprocessDifferentialsInMathML(String mathml) { // 匹配 <mi>d</mi><mi>x</mi> → <mo mathvariant="normal">d</mo><mi>x</mi> Pattern p = Pattern.compile( "(<mi[^>]*>)(d)(</mi>\\s*<mi[^>]*>)([a-zA-Z])", Pattern.CASE_INSENSITIVE ); Matcher m = p.matcher(mathml); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, "<mo mathvariant=\"normal\">d</mo><mspace width=\".1em\"/>" + "<mi>" + m.group(4) + "</mi>" ); } m.appendTail(sb); return sb.toString(); } ``` 这样输入到 `tokens.xsl` 后就会输出: ```latex \mathrm{d}\, x ``` --- ### ✅ 效果对比 | 输入 MathML | 当前输出 | 修改后输出 | |------------|----------|-----------| | `<mi>d</mi><mi>x</mi>` | `dx` | `\mathrm{d}\, x` | | `<mi>d</mi><mi>t</mi>` | `dt` | `\mathrm{d}\, t` | | `<mi>d</mi><mi>θ</mi>` | `d\theta` | `\mathrm{d}\,\theta` | 完美解决粘连和字体问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值