基于对象,应使用xslt取相应的值

时间:2016-10-02 14:16:22

标签: xslt-1.0

INPUT XML

    <root>
       <file1>
          <commodity>
              <units>1</units>
              <obj>mango</obj>
          </commodity>
          <commodity>
              <units>5</units>
              <obj>guava</obj>
           </commodity>
       </file1>

       <file2>
          <category>
              <object>guava</object>
              <type>CAT1</type>
              <colour>green</colour>
          </category>
          <category>
              <object>mango</object>
              <type>CAT2</type>
              <colour>yellow</colour>
          </category>
       </file2>
   </root>

我需要比较file1中的obj和root2下的file2中的对象的值,如果相同,我需要使用相应的单位,类型和颜色,并使用xslt生成以下输出。

输出XML

    <output>
        <com>
            <name>guava</name>
            <num>5</num>
            <category>CAT1</category>
            <col>green</col>
        </com>
        <com>
            <name>mango</name>
            <num>1</num>
            <category>CAT2</category>
            <col>yellow</col>
        </com>
     </output>

我尝试了下面的XSLT,但响应并不像预期的那样。它没有正确循环。你能告诉我我哪里出错了。

  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   version="1.0">
  <xsl:output method="xml" version="1.0" encoding="UTF-8"
    indent="yes" />
  <xsl:key name="object-search" match="root/file1/commodity" use="obj" />
  <xsl:template match="/">
    <output>
        <xsl:for-each select="key('object-search', //category/object)">
            <com>

                <name>
                    <xsl:value-of select="obj" />
                </name>
                <num>
                    <xsl:value-of select="units" />
                </num>
                <category>
                    <xsl:value-of
                        select="//root/file2/category/type" />
                </category>
                <col>
                    <xsl:value-of
                        select="//root/file2/category/colour" />
                </col>

            </com>
        </xsl:for-each>
    </output>
  </xsl:template>
  </xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

以这种方式尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="cat" match="category" use="object" />

<xsl:template match="/root">
    <output>
        <xsl:for-each select="file1/commodity">
            <com>
                <name>
                    <xsl:value-of select="obj" />
                </name>
                <num>
                    <xsl:value-of select="units" />
                </num>
                <xsl:variable name="cat" select="key('cat', obj)" />
                <category>
                    <xsl:value-of select="$cat/type" />
                </category>
                <col>
                    <xsl:value-of select="$cat/colour" />
                </col>
            </com>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

请注意,结果与您发布的内容略有不同:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <com>
      <name>mango</name>
      <num>1</num>
      <category>CAT2</category>
      <col>yellow</col>
   </com>
   <com>
      <name>guava</name>
      <num>5</num>
      <category>CAT1</category>
      <col>green</col>
   </com>
</output>

或者,您可以这样做:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="com" match="commodity" use="obj" />

<xsl:template match="/root">
    <output>
        <xsl:for-each select="file2/category">
            <xsl:variable name="com" select="key('com', object)" />
            <com>
                <name>
                    <xsl:value-of select="$com/obj" />
                </name>
                <num>
                    <xsl:value-of select="$com/units" />
                </num>
                <category>
                    <xsl:value-of select="type" />
                </category>
                <col>
                    <xsl:value-of select="colour" />
                </col>
            </com>
        </xsl:for-each>
    </output>
</xsl:template>

</xsl:stylesheet>

并获得:

<?xml version="1.0" encoding="UTF-8"?>
<output>
   <com>
      <name>guava</name>
      <num>5</num>
      <category>CAT1</category>
      <col>green</col>
   </com>
   <com>
      <name>mango</name>
      <num>1</num>
      <category>CAT2</category>
      <col>yellow</col>
   </com>
</output>