【问题标题】:Re-order XML elements in a specific way using XSLT [closed]使用 XSLT 以特定方式重新排序 XML 元素 [关闭]
【发布时间】:2013-04-02 08:53:09
【问题描述】:

我有两个 XSL 变量。 1.产品编号

  <prdid>
    <id>8143794</id>
    <id>8143793</id>
    <id>8142229</id>
    <id>8143796</id>
  </prdid>

2.Productxml

    <root>
  <product>
    <estocklevel>0</estocklevel>
    <id>8142229</id>
    <isp_brand extra="isp_brand"></isp_brand>
    <isp_produktserie extra="isp_produktserie"></isp_produktserie>
    <isp_model extra="isp_model"></isp_model>
  </product>
  <product>
    <estocklevel>0</estocklevel>
    <id>8143793</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Bred</isp_model>
  </product> 
  <product>
    <estocklevel>0</estocklevel>
    <id>8143794</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Smal</isp_model>
  </product>
  <product>
    <id>8143796</id>
    <isp_brand extra="isp_brand">Leitz</isp_brand>
    <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
    <isp_model extra="isp_model">Smal</isp_model>
  </product>
</root>

现在我想要的是,在一个 for 循环中,我想按照 prdid XML 的顺序获取 productxml 中的每个产品 喜欢

 <xsl:for-each select="prdid/id">
        <!--i want to get a product node from productxml here which got same id of that in this for loop -->
      <xsl:value-of select="productxml/id"/>
    </xsl:for-each>

【问题讨论】:

    标签: asp.net xml xslt xslt-1.0


    【解决方案1】:

    这种转换是短暂而高效的

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:variable name="vProds" select=
       "document('file:///c:/temp/delete/products.xml')"/>
    
     <xsl:key name="kProdById" match="product" use="id"/>
    
     <xsl:template match="/*">
      <root><xsl:apply-templates/></root>
     </xsl:template>
    
     <xsl:template match="id">
      <xsl:variable name="vId" select="."/>
      <xsl:for-each select="$vProds">
       <xsl:copy-of select="key('kProdById', $vId)"/>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的“prdid”XML 文档时

    <prdid>
        <id>8143794</id>
        <id>8143793</id>
        <id>8142229</id>
        <id>8143796</id>
    </prdid>
    

    第二个提供的文档包含在文件中:c:\temp\delete\products.xml:

    <root>
        <product>
            <estocklevel>0</estocklevel>
            <id>8142229</id>
            <isp_brand extra="isp_brand"></isp_brand>
            <isp_produktserie extra="isp_produktserie"></isp_produktserie>
            <isp_model extra="isp_model"></isp_model>
        </product>
        <product>
            <estocklevel>0</estocklevel>
            <id>8143793</id>
            <isp_brand extra="isp_brand">Leitz</isp_brand>
            <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
            <isp_model extra="isp_model">Bred</isp_model>
        </product>
        <product>
            <estocklevel>0</estocklevel>
            <id>8143794</id>
            <isp_brand extra="isp_brand">Leitz</isp_brand>
            <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
            <isp_model extra="isp_model">Smal</isp_model>
        </product>
        <product>
            <id>8143796</id>
            <isp_brand extra="isp_brand">Leitz</isp_brand>
            <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
            <isp_model extra="isp_model">Smal</isp_model>
        </product>
    </root>
    

    产生了想要的正确结果:

    <root>
       <product>
          <estocklevel>0</estocklevel>
          <id>8143794</id>
          <isp_brand extra="isp_brand">Leitz</isp_brand>
          <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
          <isp_model extra="isp_model">Smal</isp_model>
       </product>
       <product>
          <estocklevel>0</estocklevel>
          <id>8143793</id>
          <isp_brand extra="isp_brand">Leitz</isp_brand>
          <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
          <isp_model extra="isp_model">Bred</isp_model>
       </product>
       <product>
          <estocklevel>0</estocklevel>
          <id>8142229</id>
          <isp_brand extra="isp_brand"/>
          <isp_produktserie extra="isp_produktserie"/>
          <isp_model extra="isp_model"/>
       </product>
       <product>
          <id>8143796</id>
          <isp_brand extra="isp_brand">Leitz</isp_brand>
          <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
          <isp_model extra="isp_model">Smal</isp_model>
       </product>
    </root>
    

    解释

    正确使用keys

    这演示了如何在 XSLT 1.0 中使用来自一个 XML 文档的值作为另一个 XML 文档中节点的键

    【讨论】:

    • 正如我在问题中所说的,我有两个变量保存了这些数据。它们都在同一个 xsl 模板中。这使我无法将 vProds 声明为全局变量。你能给我一个解决方案吗
    【解决方案2】:

    我假设“Productxml”是您的输入 XML 文件。

    样式表

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:my="my:my">
      <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    
      <!--
      The order in which you want to process the <product> elements. You could also
      have this in a separate file if you want, and change the document() function
      below to point to that file instead.
      -->
      <my:prdid>
        <id>8143794</id>
        <id>8143793</id>
        <id>8142229</id>
        <id>8143796</id>
      </my:prdid>
    
      <!-- Identity template -->
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="root">
        <!-- Save reference to the nodeset that contains all <product> children -->
        <xsl:variable name="products" select="product"/>
    
        <xsl:copy>
          <!--
          For each <id> element in the <my:prdid> element (document('') refers to
          the current stylesheet)...
          -->
          <xsl:for-each select="document('')/*/my:prdid/id">
            <!--
            ...apply the <product> element that has the same id value as this <id>
            element
            -->
            <xsl:apply-templates select="$products[id = current()]"/>
          </xsl:for-each>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    输入

    <root>
      <product>
        <estocklevel>0</estocklevel>
        <id>8142229</id>
        <isp_brand extra="isp_brand"></isp_brand>
        <isp_produktserie extra="isp_produktserie"></isp_produktserie>
        <isp_model extra="isp_model"></isp_model>
      </product>
      <product>
        <estocklevel>0</estocklevel>
        <id>8143793</id>
        <isp_brand extra="isp_brand">Leitz</isp_brand>
        <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
        <isp_model extra="isp_model">Bred</isp_model>
      </product> 
      <product>
        <estocklevel>0</estocklevel>
        <id>8143794</id>
        <isp_brand extra="isp_brand">Leitz</isp_brand>
        <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
        <isp_model extra="isp_model">Smal</isp_model>
      </product>
      <product>
        <id>8143796</id>
        <isp_brand extra="isp_brand">Leitz</isp_brand>
        <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
        <isp_model extra="isp_model">Smal</isp_model>
      </product>
    </root>
    

    输出

    <root>
      <product>
        <estocklevel>0</estocklevel>
        <id>8143794</id>
        <isp_brand extra="isp_brand">Leitz</isp_brand>
        <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
        <isp_model extra="isp_model">Smal</isp_model>
      </product>
      <product>
        <estocklevel>0</estocklevel>
        <id>8143793</id>
        <isp_brand extra="isp_brand">Leitz</isp_brand>
        <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
        <isp_model extra="isp_model">Bred</isp_model>
      </product>
      <product>
        <estocklevel>0</estocklevel>
        <id>8142229</id>
        <isp_brand extra="isp_brand"/>
        <isp_produktserie extra="isp_produktserie"/>
        <isp_model extra="isp_model"/>
      </product>
      <product>
        <id>8143796</id>
        <isp_brand extra="isp_brand">Leitz</isp_brand>
        <isp_produktserie extra="isp_produktserie">180</isp_produktserie>
        <isp_model extra="isp_model">Smal</isp_model>
      </product>
    </root>
    

    【讨论】:

    • 你能告诉我我会在哪里收到这个 xslt.means 的输出在哪个变量中?
    • 我在 for 循环中做了一些小改动,因为“my”在我的样式表中出错了。我当前的 for 循环看起来像
    【解决方案3】:

    xsl:sort 应该是你需要的。 看这里:

    http://www.w3schools.com/xsl/el_sort.asp#gsc.tab=0

    编辑 1:这应该可以工作

    <xsl:for-each select="//prdid/id">
      <xsl:call-template name="sortedId">
        <xsl:with-param name="id" select="."></xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>    
    
    
    <xsl:template name="sortedId" >
        <xsl:param name="id"></xsl:param>
        <xsl:apply-templates select="//product[id=$id]" />
    </xsl:template>
    
    <xsl:template match="product">
        <xsl:copy-of select="." />
    </xsl:template>
    

    【讨论】:

    • 我不想将我的 xml 排序为升序或降序。我希望它按另一个产品 ID 列表的顺序排序
    • 编辑了我的答案。现在它应该可以工作了。也许它甚至可以用更少的代码来编写。
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多