本文介绍了使用 xslt 根据属性值过滤 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里搜索过,但找不到如何根据其属性过滤 xml.我有这个 xml:

<?xml version="1.0" encoding="utf-8"?><文档><document_head><title>这是标题</title><version>这是标题</version></document_head><document_body><段落id="AXD">这是应该在结果中的文本<属性><size>13px</size><color>#000000</color></属性><author>当前用户</author></段落><段落id="SFI"><属性>这是一些不应该在那里的其他文字</属性></段落><段落id="SFA"><author>一些随机的家伙</author></段落><段落id="ARG">这不代表什么.</段落><段落id="RRR">这确实如此,因此应该在那里.</段落></document_body></文档>

我期待这个结果:

<?xml version="1.0" encoding="UTF-8"?><文档><document_head><title>这是标题</title><version>这是标题</version></document_head><document_body><段落id="AXD">这是应该在结果中的文本<属性><size>13px</size><color>#000000</color></属性><author>当前用户</author></段落><段落id="RRR">这确实如此,因此应该在那里.</段落></document_body></文档>

目前,我有这个 XSLT:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" indent="yes"/><xsl:template match="@* | node()"><xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy></xsl:模板><xsl:template match="document_body/paragraph[not(@id='AXD')][not(@id='RRR')]"/></xsl:stylesheet>

生成此 XML 的内容:

<?xml version="1.0" encoding="UTF-8"?><文档><document_head><title>这是标题</title><version>这是标题</version></document_head><document_body><段落id="AXD">这是应该在结果中的文本<属性><size>13px</size><color>#000000</color></属性><author>当前用户</author></段落></document_body></文档>

你知道我错过了什么吗?

谢谢.

更新:该代码似乎适用于另一个 XSLT 处理器,但不适用于 Java Transformer.

解决方案

我相信你的条件应该可行!但是,这里有几种可供选择的检查方法,因此请试一试,看看这是否有所不同.

<xsl:template match="document_body/paragraph[not(@id='AXD' or @id='RRR')]"/><xsl:template match="document_body/paragraph[@id != 'AXD' and @id != 'RRR']"/>

I have searched here and I'm not able to find how to filter an xml based on their attribute. I have this xml:





     <?xml version="1.0" encoding="utf-8"?>
        <document>
            <document_head>
                <title>This is the title</title>
                <version>This is the title</version>
            </document_head>
            <document_body>
                <paragraph id="AXD">
                    <text>
                        This is a text that should be in the result
                    </text>
                    <properties>
                        <size>13px</size>
                        <color>#000000</color>
                    </properties>
                    <author>Current user</author>
                </paragraph>
                <paragraph id="SFI">
                    <properties>
                        <text>
                            This is some other text that should not be in there
                        </text>
                    </properties>
                </paragraph>
                <paragraph id="SFA">
                    <author>Some random guy</author>
                </paragraph>
                <paragraph id="ARG">
                    This doesn't mean anything.
                </paragraph>
                <paragraph id="RRR">
                    This does, hence should be in there.
                </paragraph>
            </document_body>
        </document>


I expect this result:



    <?xml version="1.0" encoding="UTF-8"?>
    <document>
        <document_head>
            <title>This is the title</title>
            <version>This is the title</version>
        </document_head>
        <document_body>
            <paragraph id="AXD">
                <text>
                    This is a text that should be in the result
                </text>
                <properties>
                    <size>13px</size>
                    <color>#000000</color>
                </properties>
                <author>Current user</author>
            </paragraph>
            <paragraph id="RRR">
                This does, hence should be in there.
            </paragraph>
        </document_body>
    </document>


Currently, I have this XSLT:



    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
      </xsl:template>

      <xsl:template match="document_body/paragraph[not(@id='AXD')][not(@id='RRR')]" />
    </xsl:stylesheet>


Which produces this XML:





     <?xml version="1.0" encoding="UTF-8"?>
        <document>
            <document_head>
                <title>This is the title</title>
                <version>This is the title</version>
            </document_head>
            <document_body>
                <paragraph id="AXD">
                    <text>
                        This is a text that should be in the result
                    </text>
                    <properties>
                        <size>13px</size>
                        <color>#000000</color>
                    </properties>
                    <author>Current user</author>
                </paragraph>
            </document_body>
        </document>


Do you know what I am missing?

Thanks.

Update: It seems that the code works for another XSLT processor, but it doesn't for Java Transformer.

解决方案

I am sure your condition should work! However, here is couple of alternative ways to check, so give this a go instead to see if that makes a difference.

<xsl:template match="document_body/paragraph[not(@id='AXD' or @id='RRR')]"/>

<xsl:template match="document_body/paragraph[@id != 'AXD' and @id != 'RRR']"/>

这篇关于使用 xslt 根据属性值过滤 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:07