如何使用 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 .xml
和 document_2.xml
到 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
我需要如下输出
<products>
<product>
<name>Bag</name>
</product>
<product>
<name>Pencil</name>
</product>
<product>
<name>Pen</name>
</product>
</product>
below is the 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>
and document_2.xml
is
<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>
and document.xml
is
<products>
</products>
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>
The output of above xsl is
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
匹配
Quantity
的空模板应该可以做到这一点:An empty template matching
Quantity
should do it:替换:
为:
或者,如果
Quantity
不是product
的直接子级,而是后代,则将上面的内容替换为:并添加以下模板:
注意:
虽然这些更改产生了新的想要的结果,但许多先前的处理变得不必要,可以完全省略。
转换可以大大简化为(没有找到最小数量,只是根据产品名称进行分组):
Replace:
with:
Or, if
Quantity
isn't a direct child ofproduct
, but a descendant, then replace the above with:and also add the following templates:
Do notice:
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):
尝试添加
Try adding