【问题标题】:Need help in XSLT Transformation在 XSLT 转换方面需要帮助
【发布时间】:2015-05-18 13:36:09
【问题描述】:

这是输入的 xml。

<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. 将元素值转换为元素名称。例如:&lt;category_id&gt;frame-shape_oval&lt;/category_id&gt; 变为 &lt;frame_shape&gt;oval&lt;/frame_shape&gt;。所以下划线之前的文本成为元素名称,下划线之后的文本成为元素值。
  2. 请注意,元素 &lt;category_id&gt; frame-shape 和 frame-color 在 &lt;product&gt; 中重复,但在 &lt;category_name&gt; 中具有不同的值。这些值用逗号连接。

【问题讨论】:

标签: xml xslt


【解决方案1】:

第一个问题比较简单,可以通过以下方式处理:

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 以及 SO 上的许多 Muenchian 分组示例。

【讨论】:

    【解决方案2】:

    您可以尝试以下解决方案。请注意,我使用了 microsoft 的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 2022-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-27
      相关资源
      最近更新 更多