使用xslt 2.0在属性和文本内容匹配时从xml中删除节点
我在转换xml时遇到困难。以下是我的xml:使用xslt 2.0在属性和文本内容匹配时从xml中删除节点,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我在转换xml时遇到困难。以下是我的xml: <?xml version="1.0" encoding="UTF-8"?> <root> <organisation> <school> <name>school of arts Berlin</name> <address>123street</address> &
<?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>
</teacher>
<teacher id="A254">
<situation>
<ill>no</ill>
</situation>
</teacher>
<teacher id="B254">
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="X92">
<situation>
<ill>no</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>probable</ill>
</situation>
</teacher>
<teacher id="G56">
<situation>
<ill>yes</ill>
</situation>
</teacher>
</teachers>
</root>
柏林艺术学院
123街
34A
14-09-2018
美国能源部
对
不
可能的
不
可能的
对
我正在努力实现的目标:
- teacher元素有一个属性id,如果它以“A2”开头,并且同一teacher节点中元素ill的文本内容等于“yes”,则必须删除teacher节点
- 教师元素有一个属性id,如果它以“G5”开头,并且同一教师节点中元素ill的文本内容等于“可能”,则必须删除教师节点
<?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>
</teacher>
<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>
柏林艺术学院
123街
34A
14-09-2018
美国能源部
不
可能的
不
对
到目前为止,我还没能做到这一点。我在上面写的第一条(要求)上卡住了。这是我的xslt:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--xsl:template match="teachers"-->
<xsl:output omit-xml-declaration="yes"/>
<xsl:param name="teacher-to-remove" select="'yes'"/>
<xsl:template match="node()|@*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="teacher">
<xsl:if test="(not(contains(concat(',', $teacher-to-remove, ','), concat(',', situation/ill, ','))) and not(starts-with(@id, 'A2')))">
<xsl:call-template name="identity"/>
</xsl:if>
</xsl:template>
鉴于这一结果:
<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>probable</ill>
</situation>
</teacher>
</teachers>
</root>
柏林艺术学院
123街
34A
14-09-2018
美国能源部
可能的
不
可能的
包含元素的所有教师节点
yes
被删除,这是不正确的;id为A254的所有教师节点也被删除,这也是不正确的。xsl:if条件没有按照我预期(或想要的)的方式工作。非常感谢您的帮助。您可以使用两个空模板来实现这一点:
<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:template match="teacher[starts-with(@id,'A2') and situation/ill='yes']" />
<xsl:template match="teacher[starts-with(@id,'G5') and situation/ill='probable']" />
它们会过滤掉不需要的元素。此条件节点删除不需要2.0。此条件节点删除不需要2.0。谢谢您的回答。不幸的是,我犯了一个致命的错误,一个教师节点中可以有多个情境元素。我已经编辑了这个问题。我道歉。你能再看一下我的问题吗?这违反了SO的规定。你应该接受这个(正确的)答案,并在新的条件下创建一个新问题。否则,它将在当前问题和当前答案的状态之间造成混乱。这显然是不可取的。所以这个问题应该被回滚。同意,我会问一个新问题。谢谢你的回答。不幸的是,我犯了一个致命的错误,一个教师节点中可以有多个情境元素。我已经编辑了这个问题。我道歉。你能再看一下我的问题吗?这违反了SO的规定。你应该接受这个(正确的)答案,并在新的条件下创建一个新问题。否则,它将在当前问题和当前答案的状态之间造成混乱。这显然是不可取的。所以这个问题应该退一步。同意,我会问一个新问题。