【发布时间】:2023-03-26 12:54:01
【问题描述】:
我在转换 xml 时遇到了困难。这是我的xml:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<organisation>
<school>
<name>school of arts Berlin</name>
<address>123street</address>
</school>
</organisation>
<teachers>
<wo_number>34A</wo_number>
<publication>
<date>14-09-2018</date>
<name>J. doe</name>
</publication>
<teacher id="A254">
<situation>
<ill>yes</ill>
</situation>
<situation>
<ill>no</ill>
</situation>
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="A254">
<situation>
<ill>yes</ill>
</situation>
<situation>
<ill>yes</ill>
</situation>
</teacher>
<teacher id="B254">
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="X92">
<situation>
<ill>no</ill>
</situation>
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>probable</ill>
</situation>
<situation>
<ill>no</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>yes</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>probable</ill>
</situation>
</teacher>
</teachers>
</root>
我想要达到的目标:
- teacher 元素有一个属性 id,如果它以“A2”开头并且同一教师节点中的元素 ill 的文本内容等于“yes”,则必须删除情境节点。如果教师节点中没有情景节点,则必须删除教师节点
- teacher 元素有一个属性 id,如果它以“G5”开头并且同一教师节点中的元素 ill 的文本内容等于“probable”,则必须删除情境节点。如果教师节点中没有情景节点,则必须删除教师节点
正确的结果应该是:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<organisation>
<school>
<name>school of arts Berlin</name>
<address>123street</address>
</school>
</organisation>
<teachers>
<wo_number>34A</wo_number>
<publication>
<date>14-09-2018</date>
<name>J. doe</name>
</publication>
<teacher id="A254">
<situation>
<ill>no</ill>
</situation>
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="B254">
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="X92">
<situation>
<ill>no</ill>
</situation>
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>no</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>yes</ill>
</situation>
</teacher>
</teachers>
</root>
到目前为止,我还没有做到这一点。我的 xslt 现在是:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="teacher[starts-with(@id,'A2') and situation/ill='yes']"/>
<xsl:template match="teacher[starts-with(@id,'G5') and situation/ill='probable']"/>
</xsl:stylesheet>
有了这个结果:
<root>
<organisation>
<school>
<name>school of arts Berlin</name>
<address>123street</address>
</school>
</organisation>
<teachers>
<wo_number>34A</wo_number>
<publication>
<date>14-09-2018</date>
<name>J. doe</name>
</publication>
<teacher id="B254">
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="X92">
<situation>
<ill>no</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>yes</ill>
</situation>
</teacher>
</teachers>
</root>
所有教师节点
id="A254" 被删除,这是不正确的,带有id="G56" 的教师节点也被删除,这也是不正确的。非常感谢一些帮助。
【问题讨论】: