【发布时间】:2013-06-12 09:58:05
【问题描述】:
我目前正在将 XML 从一个 CMS 迁移到另一个,并且需要将一些文本转换为元素。由于系统的工作原理,一些编辑器只能输入转义文本。挑战是替换其中一些转义元素并将它们转换为有效的 XML 元素。
源文件:
<p>Press the <button-name>Select key </button-name>to show more information.</p>
<p>Press the <button-name>Back key</button-name> to save the
values.</p>
<p>When the storage is completed, the <product-name/> machine
displays:</p>
<p><attention>
<display-text translate="no">STORAGE COMPLETED
Press BACK to exit</display-text>
</attention></p>
我想做的事
Replace <button-name> with <gui>
Replace <button-name> with <kt.in name="custom-name"/>
Keeping other escaped elements.
我想要的 XML
<p>Press the <gui>Select key</gui>to
show more information.</p>
<p>Press the <gui>Back key</gui>
to save the calibrations values.</p>
<p>When the storage is completed, the <kt.in name="custom-name"/> machine
displays:</p>
<p><attention> <display-text translate="no">STORAGE COMPLETED
Press BACK to exit</display-text>
</attention></p>
我尝试使用基于字符串的搜索和替换,但因为我想要一个正确的 XML 元素作为输出,所以不会这样做。
【问题讨论】:
-
您使用哪种 XSLT 处理器?在我看来,正确的方法是使用扩展来解析
p元素的内容,然后将模板应用到解析的节点并根据需要进行转换并序列化回来(对于attention)。然而,这种方法是否可行取决于所使用的 XSLT 处理器以及是否有可用于解析和序列化的扩展。 -
我通过 AntillesXML 使用 Xalan。解析所有然后序列化返回是一个好主意。我会试试看!
标签: xslt