【发布时间】:2013-11-25 18:19:54
【问题描述】:
我正在使用 boost 序列化来实现持久性,并且由于该库不支持保存到旧版本的存档/数据结构的想法,但我想给 XSLT 和 XPath 一个转换的机会根据需要从新版本到旧版本。
我已经完成了大约一半,但似乎无法完成(这也是我第一次尝试 XSLT 和 XPath/XQuery,所以请原谅任何明显的错误)。
这是我的起始 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="7">
<tester class_id="0" tracking_level="0" version="0">
<count>2</count>
<item_version>0</item_version>
<item class_id="2" class_name="CLASS_D" tracking_level="0" version="0">
<A class_id="1" tracking_level="1" version="0" object_id="_0">
<pimpl class_id="3" tracking_level="1" version="0" object_id="_1">
<b>1</b>
</pimpl>
</A>
<pimpl class_id="4" tracking_level="1" version="0" object_id="_2">
<c>2</c>
</pimpl>
</item>
<item class_id="5" class_name="CLASS_E" tracking_level="0" version="0">
<A object_id="_3">
<pimpl class_id_reference="3" object_id="_4">
<b>1</b>
</pimpl>
</A>
<pimpl class_id="6" tracking_level="1" version="0" object_id="_5">
<f>2</f>
</pimpl>
</item>
</tester>
</boost_serialization>
我想要做的是将具有属性 class_name="CLASS_E" 的项目转换为具有 class_name="CLASS_D" 的项目,但我需要单独保留 object_id 属性。
这就是我想要的:
<?xml version="1.0" encoding="utf-8"?>
<boost_serialization signature="serialization::archive" version="7">
<tester class_id="0" tracking_level="0" version="0">
<count>2</count>
<item_version>0</item_version>
<item class_id="2" class_name="CLASS_D" tracking_level="0" version="0">
<A class_id="1" tracking_level="1" version="0" object_id="_0">
<pimpl class_id="3" tracking_level="1" version="0" object_id="_1">
<b>1</b>
</pimpl>
</A>
<pimpl class_id="4" tracking_level="1" version="0" object_id="_2">
<c>2</c>
</pimpl>
</item>
<item class_name="CLASS_D" class_id="2" tracking_level="0" version="0">
<A object_id="_3">
<pimpl class_id_reference="3" object_id="_4">
<b>1</b>
</pimpl>
</A>
<pimpl class_id="4" tracking_level="1" version="0" object_id="_5">
<c>2</c>
</pimpl>
</item>
</tester>
</boost_serialization>
这是我目前的模板:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" encoding="UTF-8" indent="yes"/>
<!-- identity-->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- replace attribute class_name value with another-->
<!-- replace attribute class_id value with another-->
<!-- only on this node!-->
<!-- could call another template to change more nested things-->
<xsl:template match="item/@class_name[. = 'CLASS_E']">
<xsl:attribute name="class_name">CLASS_D</xsl:attribute>
<xsl:attribute name="class_id">2</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
我不确定如何继续编辑与此行匹配的项目的子节点: 因为我需要将“f”节点更改为“c”并将 pimpl “class_id”从 6 更改为 4
提前致谢
【问题讨论】:
标签: xml xslt xpath boost-serialization