-1

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

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

2.产品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>

现在我想要的是,在一个 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>
4

3 回答 3

2

这种转换简短而有效

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

说明

正确使用钥匙

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

于 2013-04-03T03:17:06.880 回答
1

我假设“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>
于 2013-04-02T12:57:31.087 回答
0

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>
于 2013-04-02T08:57:52.463 回答