2015-05-18 78 views
0

这是输入xml。在XSLT转换中需要帮助

<catalog> 
    <product> 
     <product_id>1234</product_id> 
     <categories> 
      <category> 
       <category_id>frame-shape_oval</category_id> 
       <category_name>frame-shape_oval</category_name> 
      </category> 
      <category> 
       <category_id>frame-shape_square</category_id> 
       <category_name>frame-shape_square</category_name> 
      </category> 
      <category> 
       <category_id>frame-color_tortoise</category_id> 
       <category_name>frame-color_tortoise</category_name> 
      </category> 
      <category> 
       <category_id>face-shape_oval</category_id> 
       <category_name>face-shape_oval</category_name> 
      </category> 
      <category> 
       <category_id>face-shape_square</category_id> 
       <category_name>face-shape_square</category_name> 
      </category> 
      <category> 
       <category_id>gender_men</category_id> 
       <category_name>gender_men</category_name> 
      </category> 
      <category> 
       <category_id>lens-color_gold rose</category_id> 
       <category_name>lens-color_gold rose</category_name> 
      </category> 
      <category> 
       <category_id>fit_average</category_id> 
       <category_name>fit_average</category_name> 
      </category> 
     </categories> 
    </product> 
</catalog> 

这是预期的转变

<catalog> 
    <product> 
     <product_id>1234</product_id> 
     <frame_shape>oval,square</frame_shape> 
     <frame_color>tortoise</frame_color> 
     <face_shape>oval,square</face_shape> 
     <gender>men</gender> 
     <lens_color>gold rose</lens_color> 
     <fit>average</fit> 
    </product> 
</catalog> 

这是可以通过XSLT转换?

  1. 将元素值转换为元素名称。例如:<category_id>frame-shape_oval</category_id>变为<frame_shape>oval</frame_shape>。因此,下划线之前的文本成为下划线变为元素值后的元素名称和文本。
  2. 请注意,元素<category_id>的框架形状和框架颜色重复用于<product>,但在<category_name>中有不同的值。这些值与逗号连接。
+0

请指出您是否可以使用XSLT 2.0或仅使用1.0。 –

+0

这将是XSLT 1.0 – Sarang

+0

@Sarang http://stackoverflow.com/help/someone-answers –

回答

-1

您可以尝试以下解决方案。请注意,我已经使用微软的node-set()将“temp”变量的内容转换为节点集。

<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:template match="/*"> 
     <catalog> 
      <xsl:for-each select="product"> 
      <product> 
       <xsl:copy-of select="product_id"/> 
       <xsl:variable name="temp"> 
       <xsl:for-each select="categories/category//category_id"> 
        <xsl:element name="{substring-before(.,'_')}"> 
        <xsl:value-of select="substring-after(.,'_')"/> 
        </xsl:element> 
       </xsl:for-each> 
       </xsl:variable> 
       <xsl:for-each select="msxsl:node-set($temp)/*"> 
       <xsl:variable name="curName" select="local-name(.)"/> 
       <xsl:choose> 
        <xsl:when test="(count(following-sibling::*[local-name()=$curName]) &gt; 0) and (count(preceding-sibling::*[local-name()=$curName]) = 0)"> 
        <xsl:element name="{$curName}"> 
         <xsl:value-of select="."/> 
         <xsl:for-each select="following-sibling::*[local-name()=$curName]"> 
         ,<xsl:value-of select="."/> 
         </xsl:for-each> 
        </xsl:element> 
        </xsl:when> 
        <xsl:when test="(count(following-sibling::*[local-name()=$curName]) = 0) and (count(preceding-sibling::*[local-name()=$curName]) = 0)"> 
        <xsl:element name="{$curName}"> 
         <xsl:value-of select="."/> 
        </xsl:element> 
        </xsl:when> 
       </xsl:choose> 
       </xsl:for-each> 
      </product> 
      </xsl:for-each> 
     </catalog> 
     </xsl:template> 
</xsl:stylesheet> 
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:strip-space elements="*"/> 

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

<xsl:template match="category"> 
    <xsl:element name="{substring-before(category_id, '_')}"> 
     <xsl:value-of select="substring-after(category_id, '_')"/> 
    </xsl:element> 
</xsl:template> 

</xsl:stylesheet> 

注意,这个假设子在category_id之前强调将永远是一个有效的XML元素名称。

对于第二个问题,请参阅http://www.jenitennison.com/xslt/grouping/muenchian.html以及Muenchian在SO上分组的许多例子。