【问题标题】:xslt 1.0 add new elementsxslt 1.0 添加新元素
【发布时间】:2016-12-03 14:03:03
【问题描述】:

下面是输入的xml:

<car>
  <colors>R+G+B</colors>
</car>

我想改成:

<car>
  <colors>R</colors>
  <colors>G</colors>
  <colors>B</colors>
</car>

原始颜色元素的值可以是 R、G 和 B 的任意组合。我的策略是在第一个节点之后为每个颜色值添加一个新元素。

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

<xsl:template match="/ImageProductOrder/color">
 //insert another color element here 
</xsl:template>

我不确定如何通过 XSLT 实际实现这一点。或者是否有其他策略可以让它发挥作用?

【问题讨论】:

  • 这适用于 XSLT 1.0
  • "可以是 R、G 和 B 的任意组合。" 是否预先知道所有可能的值 R、G 和 B? -- "This is for XSLT 1.0" 特别是哪个 XSLT 1.0 处理器?
  • 是的,这些值只是 R、G 和 B 的所有组合

标签: xml xslt


【解决方案1】:

这些值只是 R、G 和 B 的所有组合

好吧,那么你可以这样做:

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="colors">
    <xsl:if test="contains(., 'R')">
        <colors>R</colors>
    </xsl:if>
    <xsl:if test="contains(., 'B')">
        <colors>B</colors>
    </xsl:if>
    <xsl:if test="contains(., 'G')">
        <colors>G</colors>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    更通用的方法是使用递归模板在(可配置的)分隔符上进行拆分:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
        <xsl:output indent="yes"/>
    
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="colors">
            <xsl:call-template name="make-color">
                <xsl:with-param name="val" select="."/>
            </xsl:call-template> 
        </xsl:template>
    
        <xsl:template name="make-color">
            <xsl:param name="val"/>
            <xsl:param name="delim" select="'+'"/>
            <xsl:choose>
                <xsl:when test="contains($val, $delim)">
                    <color><xsl:value-of select="substring-before($val, $delim)"/></color>
                    <xsl:call-template name="make-color">
                        <xsl:with-param name="val" select="substring-after($val, $delim)"/>
                        <xsl:with-param name="delim" select="$delim"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <color><xsl:value-of select="$val"/></color>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 1970-01-01
      相关资源
      最近更新 更多