使用xslt从xml中删除specfic节点

时间:2015-07-23 11:15:41

标签: xml xslt

使用xslt

从xml中删除特定节点

我的问题有两个部分

1)使用xslt

从xml中删除specfic节点

2)一旦xslt准备就绪,这必须在运行时完成,要删除的节点值将作为参数传递

         <?xml version="1.0" encoding="UTF-8"?>
<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Header>
        <Category>History</Category>        
    </Header>
    <Body  messagetype="Personal">
        <Books>
            <Book>
                <authorDetails>
                    <title>The white tiger</title>
                    <author>Arvind Adiga</author>                   
                </authorDetails>                
                    <sellerDetails sellerType="Private">
                        <address >
                            <country>India</country>
                            <city>chennai</city>
                            </address >                     
                </sellerDetails>
                <identification>                    
                    <isbnNumber>AAAA1234</isbnNumber>                   
                </identification>
            </Book>
            <Book>
                <authorDetails>
                    <title>The white tiger</title>
                    <author>Arvind Adiga</author>                   
                </authorDetails>                
                    <sellerDetails sellerType="Private">
                        <address >
                            <country>India</country>
                            <city>chennai</city>
                            </address >                     
                </sellerDetails>
                <identification>                    
                    <isbnNumber>BBB12343</isbnNumber>                   
                </identification>
            </Book>
        </Books>
    </Body>
</Message>

以上是xml示例 isbn数字将动态传递, 并且xslt应该能够在java代码中获取isbn数字的值作为参数,并在运行时删除特定节点并将xml转换为传递的节点。

抱歉把问题放在同一个地方 如果我单独得到答案,它会很方便

到目前为止,我已设法创建此XSLt,它返回所有值

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:param name="codes"/>

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

    <xsl:template match="segment">
        <xsl:choose>
            <xsl:when test="contains($codes, code/@Value)" /> <!-- Do nothing -->
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Martin给出的答案有100分,而且效果很好, 我仍然有一个问题,我出于简单/安全原因给出了一个exmaple xml,但我正在使用的原始xml具有命名空间和 也就是这里提到的isbn数字不是直接因素

在应用xslt之后,请参阅下面的原始xml和转换后的xml 原始xml

<?xml version="1.0" encoding="UTF-8"?>
<dt:Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dt="urn:aaaaaaaa" xmlns="urn:bbbbbbb" xmlns:cm="urn:cccccccc" xmlns:stf="dddddd" ">
    <Header>
        <Category>History</Category>        
    </Header>
    <dt:Body >
        <Books>
            <Book>
                <authorDetails>
                    <cm:Birth>
                        <cm:BirthDate>
                            <cm:DateYMD>1988-01-01</cm:DateYMD>
                        </cm:BirthDate>
                    </cm:Birth>
                    <cm:Address addressType="residential">
                        <cm:CountryCode>GB</cm:CountryCode>
                        <cm:AddressStruct>
                            <cm:Street>london</cm:Street>                           
                            <cm:PostCode>sddas</cm:PostCode>
                            <cm:City>london</cm:City>
                        </cm:AddressStruct>
                    </cm:Address>
                    <cm:Name>
                        <cm:NameFree>James Bond</cm:NameFree>
                    </cm:Name>              
                </authorDetails>
                <sellerDetails>
                    <sellerDetail sellerType="Private">
                    </sellerDetail>
                </sellerDetails>
                <BookSpec>
                    <stf:BookTypeIndic>2</stf:BookTypeIndic>
                    <stf:BookRefId>AAAAAA111111111</stf:BookRefId>
                    <stf:BookCorrRefId>BBBBBB2222222222</stf:BookCorrRefId>
                </BookSpec>
            </Book>

            <Book>
                <authorDetails>
                    <cm:Birth>
                        <cm:BirthDate>
                            <cm:DateYMD>1995-01-01</cm:DateYMD>
                        </cm:BirthDate>
                    </cm:Birth>
                    <cm:Address addressType="residential">
                        <cm:CountryCode>GB</cm:CountryCode>
                        <cm:AddressStruct>
                            <cm:Street>ripper Street</cm:Street>                            
                            <cm:PostCode>sddas</cm:PostCode>
                            <cm:City>london</cm:City>
                        </cm:AddressStruct>
                    </cm:Address>
                    <cm:Name>
                        <cm:NameFree>James Bond</cm:NameFree>
                    </cm:Name>              
                </authorDetails>
                <sellerDetails>
                    <sellerDetail sellerType="Private">
                    </sellerDetail>
                </sellerDetails>
                <BookSpec>
                    <stf:BookTypeIndic>2</stf:BookTypeIndic>
                    <stf:BookRefId>CCCCCC33333333</stf:BookRefId>
                    <stf:BookCorrRefId>DDDDDD4444444</stf:BookCorrRefId>
                </BookSpec>
            </Book>


            <Book>
                <authorDetails>
                    <cm:Birth>
                        <cm:BirthDate>
                            <cm:DateYMD>1995-01-01</cm:DateYMD>
                        </cm:BirthDate>
                    </cm:Birth>
                    <cm:Address addressType="residential">
                        <cm:CountryCode>GB</cm:CountryCode>
                        <cm:AddressStruct>
                            <cm:Street>ripper Street</cm:Street>                            
                            <cm:PostCode>sddas</cm:PostCode>
                            <cm:City>london</cm:City>
                        </cm:AddressStruct>
                    </cm:Address>
                    <cm:Name>
                        <cm:NameFree>James Bond</cm:NameFree>
                    </cm:Name>              
                </authorDetails>
                <sellerDetails>
                    <sellerDetail sellerType="Private">
                    </sellerDetail>
                </sellerDetails>
                <BookSpec>
                    <stf:BookTypeIndic>2</stf:BookTypeIndic>
                    <stf:BookRefId>EEEEEE555555555</stf:BookRefId>
                    <stf:BookCorrRefId>FFFFFF66666666666</stf:BookCorrRefId>
                </BookSpec>
            </Book>     
        </Books>
    </dt:Body>
</dt:Message>

传递参数时传输的xml {“'CCCCCC33333333','EEEEEE555555555'”}

<?xml version="1.0" encoding="UTF-8"?>
<dt:Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dt="urn:aaaaaaaa" xmlns="urn:bbbbbbb" xmlns:cm="urn:cccccccc" xmlns:stf="dddddd" ">
    <Header>
        <Category>History</Category>        
    </Header>
    <dt:Body >
        <Books>
            <Book>
                <authorDetails>
                    <cm:Birth>
                        <cm:BirthDate>
                            <cm:DateYMD>1988-01-01</cm:DateYMD>
                        </cm:BirthDate>
                    </cm:Birth>
                    <cm:Address addressType="residential">
                        <cm:CountryCode>GB</cm:CountryCode>
                        <cm:AddressStruct>
                            <cm:Street>london</cm:Street>                           
                            <cm:PostCode>sddas</cm:PostCode>
                            <cm:City>london</cm:City>
                        </cm:AddressStruct>
                    </cm:Address>
                    <cm:Name>
                        <cm:NameFree>James Bond</cm:NameFree>
                    </cm:Name>              
                </authorDetails>
                <sellerDetails>
                    <sellerDetail sellerType="Private">
                    </sellerDetail>
                </sellerDetails>
                <BookSpec>
                    <stf:BookTypeIndic>2</stf:BookTypeIndic>
                    <stf:BookRefId>AAAAAA111111111</stf:BookRefId>
                    <stf:BookCorrRefId>BBBBBB2222222222</stf:BookCorrRefId>
                </BookSpec>
            </Book>     
        </Books>
    </dt:Body>
</dt:Message>

1 个答案:

答案 0 :(得分:0)

如果您使用http://xsltransform.net/eiZQaG7

Book

您可以传入包含单个ISBN编号的字符串或带有逗号分隔的ISBN编号列表的字符串,样式表只会将这些identification/isbnNumber元素复制到<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:df="urn:bbbbbbb" xmlns:stf="dddddd" exclude-result-prefixes="df stf"> <xsl:output method="xml" indent="yes"/> <xsl:strip-space elements="*"/> <xsl:param name="isbns-to-remove" select="'CCCCCC33333333,EEEEEE555555555'"/> <xsl:template match="@*|node()" name="identity"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="df:Book"> <xsl:if test="not(contains(concat(',', $isbns-to-remove, ','), concat(',', df:BookSpec/stf:BookRefId, ',')))"> <xsl:call-template name="identity"/> </xsl:if> </xsl:template> </xsl:stylesheet> 中未包含<div>的结果中参数。

由于您现在使用命名空间发布了更复杂的XML输入示例,因此需要调整XSLT代码中的路径:

<div>
    ...
    <script type="text/javascript">
        myFunction(dynam_param1, dynam_param2);
    </script>
</div>