我目前正在將XML從一個CMS遷移到另一個,並且需要將某些文本轉換爲元素。由於系統工作原理,一些編輯只能輸入轉義文本。挑戰是取代一些這些轉義元素並將其轉換爲有效的XML元素。使用XSLT將文本轉換爲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。解析所有隨後序列化回來是一個好主意。我試試看! – user2477722