如何使用 xslt 从文档方法中删除节点 [英] How remove node from the document method using xslt

查看:31
本文介绍了如何使用 xslt 从文档方法中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是document_1.xml

<products>
    <product>
        <name>Pen</name>
        <Quantity>10</Quantity>
    </product>
    <product>
        <name>Pencil</name>
        <Quantity>20</Quantity>
    </product>
    <product>
        <name>Bag</name>
        <Quantity>25</Quantity>
    </product>
</products>

document_2.xml

<products>
    <product>
        <name>Pen</name>
        <Quantity>30</Quantity>
    </product> 

    <product>
        <name>Pencil</name>
        <Quantity>5</Quantity>
    </product>
    <product>
        <name>Bag</name>
        <Quantity>2</Quantity>
    </product>
</products>

document.xml

<products>
</products>

下面是我的xsl,我曾经将document_1.xmldocument_2.xml 加入到document.xml

Below is my xsl, i used to join document_1.xml and document_2.xml to the document.xml

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="ns">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="kProdByName" match="product" use="name"/>

<xsl:template match="products">
<xsl:copy>

<xsl:variable name="msNodes">
    <xsl:apply-templates select="document('document_1.xml')/*/product|document('document_2.xml')/*/product">
    <xsl:sort select="Quantity" data-type="number"/>
    </xsl:apply-templates> 
</xsl:variable>

<xsl:apply-templates select="ns:node-set($msNodes)/product [generate-id() =  generate-id(key('kProdByName', name)[1])  ]"/>

</xsl:copy>
</xsl:template>


<xsl:template match="product">
   <product>
    <xsl:for-each select="key('kProdByName', name)">
      <xsl:if test="position() = 1">
        <xsl:copy-of select="node()"/>
      </xsl:if>
    </xsl:for-each>
   </product>
</xsl:template>

</xsl:stylesheet>

上面xsl的输出是

<products>
    <product>
        <name>Bag</name>
        <Quantity>2</Quantity>
    </product>
    <product>
        <name>Pencil</name>
        <Quantity>5</Quantity>
    </product>
    <product>
        <name>Pen</name>
        <Quantity>10</Quantity>
    </product>
    </product>

这里我需要使用 xslt 1.0 从输出中删除 节点

here i need to remove <Quantity> node from the output using xslt 1.0

我需要像下面这样的输出

i need output like below

<products>
    <product>
        <name>Bag</name>
    </product>
    <product>
        <name>Pencil</name>
    </product>
    <product>
        <name>Pen</name>
    </product>
    </product> 

推荐答案

替换:

<xsl:copy-of select="node()"/>

:

<xsl:copy-of select="node()[not(self::Quantity)]"/>

或者,如果 Quantity 不是 product 的直接子代,而是后代,则将上面的替换为:

Or, if Quantity isn't a direct child of product, but a descendant, then replace the above with:

<xsl:apply-templates mode="skipQuantity"/>

并添加以下模板:

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

<xsl:template match="Quantity" mode="skipQuantity"/>

请注意:

虽然这些更改产生了新的想要的结果,但许多先前的处理变得不必要并且可以完全省略.

While these changes produce the new wanted result, a lot of the previous processing becomes unnecessary and can be omitted completely.

转换可以显着简化为这个(没有找到最小数量,只是根据产品名称分组):

The transformation can be simplified significantly to this (no finding of minimum quantity, just grouping based on product name):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="ns">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:key name="kProdByName" match="product" use="name"/>

    <xsl:template match="products">
        <xsl:copy>
            <xsl:variable name="msNodes">
                    <xsl:copy-of select=
                    "document('document_1.xml')/*/product
                |
                     document('document_2.xml')/*/product"/>
            </xsl:variable>

            <xsl:apply-templates select=
             "ns:node-set($msNodes)/product
                  [generate-id()
                  =
                   generate-id(key('kProdByName', name)[1])
                   ]"/>
        </xsl:copy>
    </xsl:template>

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

 <xsl:template match="Quantity"/>
</xsl:stylesheet>

这篇关于如何使用 xslt 从文档方法中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆