当前位置 : 主页 > 网页制作 > xml >

使用xslt基于属性值过滤XML

来源:互联网 收集:自由互联 发布时间:2021-06-13
我在这里搜索过,我无法找到如何根据属性过滤xml.我有这个xml: ?xml version="1.0" encoding="utf-8"? document document_head titleThis is the title/title versionThis is the title/version /document_head document_body parag
我在这里搜索过,我无法找到如何根据属性过滤xml.我有这个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>

我期待这个结果:


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

目前,我有这个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>

哪个产生这个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>

你知道我错过了什么吗?

谢谢.

更新:似乎代码适用于另一个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']"/>
网友评论