【问题标题】:xsl:copy-of and xsl:attribute in one templatexsl:copy-of 和 xsl:attribute 在一个模板中
【发布时间】:2015-10-28 12:39:02
【问题描述】:

我有一个相当复杂的问题 (IMO),所以我希望我的意思很清楚。

考虑具有以下结构的 XML。

<products>
    <product exportName="XPOZER_90X120">
      <product-options>
        <option productOptionRef="xpozerFrame">
          <plan>
            <period startDate="2015-10-01">
              <price name="true" netBasePrice="34"/>
              <price name="false" netBasePrice="0"/>
            </period>
          </plan>
        </option>
      </product-options>
    </product>
</products>

该产品有两个“价格”子节点。 对于存在的每个“价格”子节点,都需要将其转换为一个“产品”节点,同时必须将 exportName 属性更改为生成的值。其余节点保持不变。

想要的输出:

<products>
    <product exportName="XPOZER_WITHFRAME90X120">
      <product-options>
        <option productOptionRef="xpozerFrame">
          <plan>
            <period startDate="2015-10-01">
              <price name="true" netBasePrice="34"/>
              <price name="false" netBasePrice="0"/>
            </period>
          </plan>
        </option>
      </product-options>
    </product>
    <product exportName="XPOZER_NOFRAME90X120">
      <product-options>
        <option productOptionRef="xpozerFrame">
          <plan>
            <period startDate="2015-10-01">
              <price name="true" netBasePrice="34"/>
              <price name="false" netBasePrice="0"/>
            </period>
          </plan>
        </option>
      </product-options>
    </product>
</products>

exportname 变成 'XPOZER_NOFRAME90X120' 和 'XPOZER_WITHFRAME90X120' 用同一个 xslt 中的文档函数计算,然后做一些转换。

我已经完成了产品节点转换,但我一直在更改 exportName 属性。它必须成为的值已经起作用,但我不知道如何在复制时实际更改 exportName 属性。

这是迄今为止的 xslt

    <xsl:stylesheet version="1.0" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
        xmlns:str="http://exslt.org/strings"
        xmlns:input="urn:input-variables"
        xmlns:my="my:my">
          <my:values>
            <xpozerFramefalse>NOFRAME</xpozerFramefalse>
            <xpozerFrametrue>WITHFRAME</xpozerFrametrue>
            <XPOZER_90X120>XPOZER_[xpozerFrame]90X120</XPOZER_90X120>
          </my:values>

  <xsl:template name="string-replace-all">
    <xsl:param name="text" />
    <xsl:param name="replace" />
    <xsl:param name="by" />
    <xsl:choose>
        <xsl:when test="contains($text, $replace)">
            <xsl:value-of select="substring-before($text,$replace)" />
            <xsl:value-of select="$by" />
            <xsl:call-template name="string-replace-all">
                <xsl:with-param name="text" select="substring-after($text,$replace)" />
                <xsl:with-param name="replace" select="$replace" />
                <xsl:with-param name="by" select="$by" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text" />
        </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


              <xsl:template match="/">
                <xsl:apply-templates select="products/product[@exportName='XPOZER_90X120']" />
              </xsl:template>  

              <xsl:template match="product[product-options/option/plan/period/price]">
                <xsl:for-each select="product-options/option/plan/period/price">
                    <xsl:variable name="vNewExportName">
                      <xsl:call-template name="priceNode" />
                    </xsl:variable>         
                    <xsl:copy-of select="../../../../.." />
                </xsl:for-each>
              </xsl:template>

              <xsl:template name="priceNode">

               <xsl:variable name="vProductOptionRef" select="../../../@productOptionRef" />
                <xsl:variable name="vSelector" select="concat($vProductOptionRef, @name)" />
                <xsl:variable name="vExportname" select="../../../../../@exportName" />
                <xsl:variable name="vNewExportName">
                    <xsl:call-template name="string-replace-all">
                        <xsl:with-param name="text" select="document('')//my:values/*[name()=$vExportname]" />
                        <xsl:with-param name="replace" select="concat(concat('[', $vProductOptionRef), ']')" />
                        <xsl:with-param name="by" select="document('')//my:values/*[name()=$vSelector]" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:value-of select="$vNewExportName" />
              </xsl:template>
    </xsl:stylesheet>

因此,带有 copy-of 的 for-each 负责将节点两次复制到转换后的 XML 中,并且通过从“my:values”中检索值来正确找到 exportname 应该更改为的值。 但我真的不知道如何将值应用于实际属性。

谁能帮我解决这个问题?这甚至可以在不依赖编码的情况下实现吗?

【问题讨论】:

  • 您能 1) 解释如何计算 exportName 2) 并为我们提供一个示例预期输出吗?谢谢。

标签: xml xslt xpath


【解决方案1】:

根据澄清进行编辑:

据我所知,你想做这样的事情:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my"
exclude-result-prefixes="my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<my:values>
    <xpozerFramefalse>NOFRAME</xpozerFramefalse>
    <xpozerFrametrue>WITHFRAME</xpozerFrametrue>
    <XPOZER_90X120>XPOZER_[xpozerFrame]90X120</XPOZER_90X120>
</my:values>

<xsl:template match="/products">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>  

<xsl:template match="product[product-options]">
    <xsl:for-each select="product-options/option/plan/period/price">
        <xsl:variable name="exportName" select="ancestor::product/@exportName" />
        <xsl:variable name="productOptionRef" select="ancestor::option/@productOptionRef" />
        <xsl:variable name="lookup-values" select="document('')//my:values/*" />
        <xsl:variable name="replace-src" select="concat($productOptionRef, @name)"/>

        <xsl:variable name="newExportName">
            <xsl:call-template name="replace">
                <xsl:with-param name="string" select="$lookup-values[name()=$exportName]"/>
                <xsl:with-param name="search-string" select="concat('[', $productOptionRef, ']')"/>
                <xsl:with-param name="replace-string" select="$lookup-values[name()=$replace-src]"/>
            </xsl:call-template>
        </xsl:variable>

        <product exportName="{$newExportName}">
            <product-options>
                <option productOptionRef="{$productOptionRef}">
                    <plan>
                        <period startDate="{../@startDate}">
                            <xsl:copy-of select="."/>
                        </period>
                    </plan>
                </option>
          </product-options>
        </product>
    </xsl:for-each>
</xsl:template>

<xsl:template name="replace">
    <xsl:param name="string"/>
    <xsl:param name="search-string"/>
    <xsl:param name="replace-string"/>
    <xsl:choose>
        <xsl:when test="contains($string, $search-string)">
            <xsl:value-of select="substring-before($string, $search-string)"/>
            <xsl:value-of select="$replace-string"/>
            <xsl:call-template name="replace">
                <xsl:with-param name="string" select="substring-after($string, $search-string)"/>
                <xsl:with-param name="search-string" select="$search-string"/>
                <xsl:with-param name="replace-string" select="$replace-string"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$string"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例,这里的结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<products>
   <product exportName="XPOZER_WITHFRAME90X120">
      <product-options>
         <option productOptionRef="xpozerFrame">
            <plan>
               <period startDate="2015-10-01">
                  <price name="true" netBasePrice="34"/>
               </period>
            </plan>
         </option>
      </product-options>
   </product>
   <product exportName="XPOZER_NOFRAME90X120">
      <product-options>
         <option productOptionRef="xpozerFrame">
            <plan>
               <period startDate="2015-10-01">
                  <price name="false" netBasePrice="0"/>
               </period>
            </plan>
         </option>
      </product-options>
   </product>
</products>

注意:我建议使用keymy:values 中查找值。

【讨论】:

  • @ServéLaurijssen 您需要就需要在此处应用的逻辑提供一些明确的规则。无效的代码(缺少模板!)不是一个好的指南。
  • @ServéLaurijssen 还要指出是使用 XSLT 1.0 还是 2.0。
  • @ServéLaurijssen 恐怕这仍然没有意义。请用文字解释如何一步一步地从“XPOZER_90X120”到“XPOZER_WITHFRAME90X120”。另请注意,您的代码引用了@productOptionRef,但您的 XML 中没有出现此类属性。
  • exportname 的值实际上并不重要,它们是正确计算的。问题是关于如何将值实际获取到 exportName 中。但是我在productOptionRef属性中添加了
  • "exportname 的值实际上并不重要,它们是正确计算的。" 我看不出来。我尝试运行您的代码,但收到错误 template string-replace-all not found
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 2021-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多