【问题标题】:Removing nodes from xml when attribute and text content match using xslt 2.0当属性和文本内容匹配时使用 xslt 2.0 从 xml 中删除节点
【发布时间】:2018-05-20 18:42:42
【问题描述】:

我在转换 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>
        </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>

我想要达到的目标:

  • teacher 元素有一个属性 id,如果它以“A2”开头并且同一教师节点中的元素的文本内容等于“yes”,则必须删除教师节点
  • teacher 元素有一个属性 id,如果它以“G5”开头并且同一教师节点中的元素的文本内容等于“可能”,则必须删除教师节点

正确的结果应该是:

<?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>

到目前为止,我还没有做到这一点。我被困在上面写的第一个(要求)bulit 上。这是我的 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>

所有有元素的教师节点 &lt;ill&gt;yes&lt;/ill&gt; 被删除,这是不正确的,并且所有 ID 为 A254 的教师节点都被删除,这也是不正确的。 xsl:if 条件没有按我预期(或想要)的方式工作。一些帮助将不胜感激。

【问题讨论】:

  • 此条件节点删除不需要 2.0。

标签: xml xslt xslt-2.0


【解决方案1】:

您可以使用两个空模板来实现这一点:

<xsl:template match="teacher[starts-with(@id,'A2') and situation/ill='yes']" />
<xsl:template match="teacher[starts-with(@id,'G5') and situation/ill='probable']" />

它们过滤掉不需要的元素。

【讨论】:

  • 感谢您的回答。这行得通,不幸的是我犯了一个致命的错误,一个教师节点可以在一个教师节点中有多个情境元素。我已经编辑了这个问题。我很抱歉。你能再看看我的问题吗?
  • 这违反了 SO 的规则。您应该接受这个(正确的)答案并使用新条件创建一个新问题。否则会在当前问题和当前答案的状态之间造成混乱。这是不可取的,这一点必须是显而易见的。所以这个问题应该回滚。
  • 同意,我会问一个新问题。
猜你喜欢
  • 2023-03-26
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-31
相关资源
最近更新 更多