Xml 在使用XSLT重复嵌套值的同时展平层次结构

Xml 在使用XSLT重复嵌套值的同时展平层次结构,xml,xslt,Xml,Xslt,我是xml转换的新手,正在尝试通过在消除嵌套字段重复的同时展平信息来转换现有的xml结构,但看起来并不是所有数据都能够转换 基于数据的一些局限性,我需要从源xml中提取一些信息,并对这些信息进行重复,同时为每个产品提供一个新的id。经过一些麻烦之后,我能够让它在一个较小的子集上工作,但我注意到,当我使用一个较大的数据子集时,我得到的条目并没有进入最终的xml XSLT文件 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/

我是xml转换的新手,正在尝试通过在消除嵌套字段重复的同时展平信息来转换现有的xml结构,但看起来并不是所有数据都能够转换

基于数据的一些局限性,我需要从源xml中提取一些信息,并对这些信息进行重复,同时为每个产品提供一个新的id。经过一些麻烦之后,我能够让它在一个较小的子集上工作,但我注意到,当我使用一个较大的数据子集时,我得到的条目并没有进入最终的xml

XSLT文件

<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 method="xml" indent="yes"/>

  <xsl:key name="category" match="/products/product/categories/category/categoryname/text()" use="substring-after(., ' &gt; ')" />
  <xsl:template match="/">
    <channel>
      <description>Testing Description</description>

      <xsl:for-each select="/products/product">
        <xsl:variable name="currentProduct" select="." />
        <xsl:choose>
          <xsl:when test="count($currentProduct/categories/category) &gt; 0">
            <xsl:for-each select="categories/category/categoryname/text()[generate-id() = generate-id(key('category', substring-after(., ' &gt; '))[1])]">
              <xsl:call-template name="output-item">
                <xsl:with-param name="product" select="$currentProduct" />
                <xsl:with-param name="category" select="substring-after(., ' &gt; ')" />
                <xsl:with-param name="category-count" select="position()" />
              </xsl:call-template>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="output-item">
              <xsl:with-param name="product" select="$currentProduct" />
              <xsl:with-param name="category-count" select="1" />
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </channel>
  </xsl:template>

  <xsl:template name="output-item">
    <xsl:param name="product" />
    <xsl:param name="category-count" />
    <xsl:param name="category" />
    <item>
      <id>
        <xsl:value-of select="$product/productid"/>_<xsl:value-of select="$category-count"/>
      </id>
      <item_group_id>
        <xsl:value-of select="$product/productid"/>
      </item_group_id>
      <product_type>
        <xsl:value-of select="$category" />
      </product_type>
    </item>
  </xsl:template>

</xsl:stylesheet>

测试说明
_
输入XML

<?xml version="1.0" encoding="utf-8" ?>
<products>
  <product>
    <productid>123</productid>
    <categories>
      <category>
        <categoryid>1</categoryid>
        <categoryname>main &gt; category-short</categoryname>
      </category>
      <category>
        <categoryid>2</categoryid>
        <categoryname>main &gt; category-medium</categoryname>
      </category>
      <category>
        <categoryid>3</categoryid>
        <categoryname>main &gt; category-large</categoryname>
      </category>
      <category>
        <categoryid>5</categoryid>
        <categoryname>main &gt; category-large</categoryname>
      </category>
    </categories>
    <image1>
      <url>image1-url</url>
    </image1>
    <image2>
      <url>image2-url</url>
    </image2>
  </product>
  <product>
    <productid>456</productid>
    <categories />
    <image1>
      <url>image1-url</url>
    </image1>
    <image2>
      <url>image2-url</url>
    </image2>
  </product>
  <product>
    <productid>789</productid>
    <categories>
      <category>
        <categoryid>1</categoryid>
        <categoryname>main &gt; category-short</categoryname>
      </category>
      <category>
        <categoryid>4</categoryid>
        <categoryname>main &gt; category-short</categoryname>
      </category>
    </categories>
    <image1>
      <url>image1-url</url>
    </image1>
    <image2>
      <url>image2-url</url>
    </image2>
  </product>
</products>

123
1.
主要类别短
2.
主类介质
3.
大类
5.
大类
图像1 url
图像2 url
456
图像1 url
图像2 url
789
1.
主要类别短
4.
主要类别短
图像1 url
图像2 url
电流输出(缺少第3项)


测试说明
123_1
123
类别短
123_2
123
类别媒介
123_3
123
大类
456_1
456
预期输出(包括第三项和重复数据消除类别)


测试说明
123_1
123
类别短
123_2
123
类别媒介
123_3
123
大类
456_1
456
789_1
789
类别短

我认为问题出在重复数据消除部分,但一直无法找到它。任何帮助都会很棒

换一种方式怎么样

XSLT1.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="category" match="category" use="concat(ancestor::product/productid, '|', substring-after(categoryname, ' &gt; '))" />

<xsl:template match="/products">
    <channel>
        <description>Testing Description</description>
        <xsl:for-each select="product">
            <xsl:variable name="id" select="productid"/>
            <!-- unique categories of this product -->
            <xsl:variable name="categories" select="categories/category[count(. | key('category', concat($id, '|', substring-after(categoryname, ' &gt; ')))[1]) = 1]"/>
            <xsl:choose>
                <xsl:when test="$categories">
                    <xsl:for-each select="$categories">
                        <item>
                            <id>
                                <xsl:value-of select="concat($id, '_', position())"/>
                            </id>
                            <item_group_id>
                                <xsl:value-of select="$id"/>
                            </item_group_id>
                            <product_type>
                                <xsl:value-of select="substring-after(categoryname, ' &gt; ')"/>                    
                            </product_type>
                        </item>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <item>
                        <id>
                            <xsl:value-of select="concat($id, '_1')"/>
                        </id>
                        <item_group_id>
                            <xsl:value-of select="$id"/>
                        </item_group_id>
                        <product_type/>
                    </item>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </channel>
</xsl:template>

</xsl:stylesheet>

测试说明

在XSLT 3中,您只需要

  <xsl:template match="products">
    <channel>
        <description>Testing Description</description>
        <xsl:apply-templates/>
    </channel>
  </xsl:template>

  <xsl:template match="product">
      <xsl:for-each-group select="." group-by="let $keys := categories/category/categoryname/substring-after(., ' &gt; ') return if (exists($keys)) then $keys else ''">
          <item>
              <id>{productid}_{position()}</id>
              <item_group_id>{productid}</item_group_id>
              <product_type>{current-grouping-key()}</product_type>
          </item>
      </xsl:for-each-group>
  </xsl:template>

测试说明
{productid}{position()}
{productid}
{current-grouping-key()}

您有
456
,因此输出。@Alejandro的for each不允许输出转到下一个产品,即使上一个产品没有类别?您是否受XSLT 1.0的约束?说出来总是有帮助的。使用2.0或3.0,这样的问题通常更容易解决。@MichaelKay是的,我只能使用XSLT 1.0,不过我应该检查XSLT 2.0或3.0是否可行。谢谢您的回复!它基于问题中的示例工作,但我调用模板的原因是因为输出项模板中有许多字段需要显示。我试图避免在两个choose语句中重复相同的字段分配。我将尝试在我的解决方案中使用您的解决方案的一部分,看看今天晚些时候是否可以使用它。如果您有公共元素要输出(例如给定示例中的
item\u group\u id
),您可以在变量中定义它们,然后在
xsl:choose
指令的每个分支中复制该变量。
<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="category" match="category" use="concat(ancestor::product/productid, '|', substring-after(categoryname, ' &gt; '))" />

<xsl:template match="/products">
    <channel>
        <description>Testing Description</description>
        <xsl:for-each select="product">
            <xsl:variable name="id" select="productid"/>
            <!-- unique categories of this product -->
            <xsl:variable name="categories" select="categories/category[count(. | key('category', concat($id, '|', substring-after(categoryname, ' &gt; ')))[1]) = 1]"/>
            <xsl:choose>
                <xsl:when test="$categories">
                    <xsl:for-each select="$categories">
                        <item>
                            <id>
                                <xsl:value-of select="concat($id, '_', position())"/>
                            </id>
                            <item_group_id>
                                <xsl:value-of select="$id"/>
                            </item_group_id>
                            <product_type>
                                <xsl:value-of select="substring-after(categoryname, ' &gt; ')"/>                    
                            </product_type>
                        </item>
                    </xsl:for-each>
                </xsl:when>
                <xsl:otherwise>
                    <item>
                        <id>
                            <xsl:value-of select="concat($id, '_1')"/>
                        </id>
                        <item_group_id>
                            <xsl:value-of select="$id"/>
                        </item_group_id>
                        <product_type/>
                    </item>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </channel>
</xsl:template>

</xsl:stylesheet>
  <xsl:template match="products">
    <channel>
        <description>Testing Description</description>
        <xsl:apply-templates/>
    </channel>
  </xsl:template>

  <xsl:template match="product">
      <xsl:for-each-group select="." group-by="let $keys := categories/category/categoryname/substring-after(., ' &gt; ') return if (exists($keys)) then $keys else ''">
          <item>
              <id>{productid}_{position()}</id>
              <item_group_id>{productid}</item_group_id>
              <product_type>{current-grouping-key()}</product_type>
          </item>
      </xsl:for-each-group>
  </xsl:template>