如何使用 xslt 从文档中减少排序的价值? [英] How to make sort less value from document using xslt?

查看:19
本文介绍了如何使用 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">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/products">
<xsl:copy>
<xsl:apply-templates select="document('document_1.xml')/*/product"/>
<xsl:apply-templates select="document('document_2.xml')/*/product"/>
</xsl:copy>
</xsl:template>

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

</xsl:stylesheet>

我需要像下面这样的输出

I need output like below

  1. 按数量 ASC 排序
  2. 和具有最小数量的独特

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

推荐答案

这个简单的转换:

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

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

<xsl:variable name="vallProds" select=
 "document('file:///c:/temp/delete/document_1.xml')/*/*
 |
  document('file:///c:/temp/delete/document_2.xml')/*/*
 "/>

 <xsl:template match="/*">
    <xsl:variable name="vrtfProds">
   <xsl:apply-templates select="$vallProds">
    <xsl:sort select="name"/>
    <xsl:sort select="Quantity" data-type="number"/>
   </xsl:apply-templates>
  </xsl:variable>

  <products>
   <xsl:for-each select="msxsl:node-set($vrtfProds)">
    <xsl:copy-of select=
     "*[generate-id() = generate-id(key('kProdByName', name)[1])]"/>
   </xsl:for-each>
  </products>
 </xsl:template>

 <xsl:template match="product">
  <xsl:copy-of select="."/>
 </xsl:template>
</xsl:stylesheet>

应用于任何 XML 文档时(可以是 document.xml,但未使用),并且提供的两个文档位于:c:\temp\delete\document_1.xmlc:\temp\delete\document_1.xml,

when applied on any XML document (could be document.xml, but this is not used), and having the two provided documents residing at: c:\temp\delete\document_1.xml and c:\temp\delete\document_1.xml,

产生想要的、正确的结果:

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

说明:

  1. 对两个文档联合的product元素进行排序通过两个键:name and 数量.

从 RTF 中提取常规树 (结果树片段) 在上面的步骤 1. 中生成并执行 慕尼黑分组 productname.

Extracting the regular tree from the RTF (Result Tree Fragment) produced in step 1. above and performing Muenchian grouping of product by name.

这篇关于如何使用 xslt 从文档中减少排序的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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