搜索元素并采取措施[关闭]

问题描述 投票:-1回答:1

猜猜你们都做得很好!!

我有一个场景,我需要做以下事情:

我有一个xml的内容如下。

`<?xml version="1.0" encoding="UTF-8" ?>
<a>
    <b>
        <c>
            <key>One</key>
            <value>Value1</value>
        </c>
        <c>
            <key>Two</key>
            <value>Value2</value>
        </c>
        <c>
            <key>Three</key>
            <value>Value3</value>
        </c>
        <c>
            <key>Four</key>
            <value>Value4</value>
        </c>
        <c>
            <key>Five</key>
            <value>Value5</value>
        </c>
    </b>
   <d>
       <e>
            <Five>Check this feild</Five>
            <Seven>It is extra</Seven>
            <tewnty>extra</tewnty>
        </e>
        <f>
            <Three>it is present</Three>
            <Five>Came again</Five>
        </f>
        <g>
            <Four>It is here</Four>
        </g>
    </d>
    <n>
        <Five>Dont check under n</Five>
        <Six>Extra under n</Six>
        <Three>Dont check under n</Three>
    </n>
</a>`

现在我想检查/a/b/c/key/text()是否作为元素名称/a/d存在

如果为true,则复制当前键/值对。如果不忽略此键/值对输出。

所以我的输出应该是这样的:

`<?xml version="1.0" encoding="UTF-8" ?>
<a>
    <b>
        <c>
            <key>Three</key>
            <value>Value3</value>
        </c>
        <c>
            <key>Four</key>
            <value>Value4</value>
        </c>
        <c>
            <key>Five</key>
            <value>Value5</value>
        </c>
    </b>
    <d>
        <e>
            <Five>Check this feild</Five>
            <Seven>It is extra</Seven>
            <tewnty>extra</tewnty>
        </e>
        <f>
            <Three>it is present</Three>
            <Five>Came again</Five>
        </f>
        <g>
            <Four>It is here</Four>
        </g>
    </d>
    <n>
        <Five>Dont check under n</Five>
        <Six>Extra under n</Six>
        <Three>Dont check under n</Three>
    </n>
</a>`

你能帮我解决一下XSLT吗?

我正在尝试if并检查节点的local-name()

我怎样才能在XSLT中实现它?

我的问题是,我如何根据元素名称=(c / key / text())删除一个节点(在我的例子中是某些值)。

我希望我的问题是具体的。

谢谢和问候钱德鲁

xslt xslt-1.0 xslt-2.0
1个回答
1
投票

下面的样式表使用xsl:key/a/d/*/*下使用local-name(),然后使用它作为标准来排除/a/b/*text()值在xsl:key中找不到任何项目的元素。

这是一个修改过的identity transformation,其中默认处理是保留所有内容。与使用xsl:key的第二个空模板匹配的元素可确保删除这些元素。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" encoding="UTF-8" />

  <xsl:key name="filter" match="/a/d/*/*" use="local-name()"/>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="/a/b/c[not(key('filter', key/text()))]"/>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.