【问题标题】:add incremental value in xslt tag在 xslt 标记中添加增量值
【发布时间】:2017-07-07 11:12:39
【问题描述】:
 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" />
  <soapenv:Body>
    <swiftResponseResponse xmlns="http://webservice.sbi.com">
      <swiftResponseReturn>15617TS006140|DC768736|13321.49|04-05-2017 15:13:03|SWIFTINR|NA|FAIL-ACCOUNT UNAVAILABLE</swiftResponseReturn>
    </swiftResponseResponse>
  </soapenv:Body>
</soapenv:Envelope>

我的 xsl 输入是

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://webservice.sbi.com" exclude-result-prefixes="soapenv xsl  xsd xsi xs ">
  <!--<xsl:output method="xml" indent="yes"/>-->
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <!--<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>-->


  <xsl:template name="tokenize">
    <xsl:param name="text"/>
    <xsl:param name="separator" />
    <xsl:choose>
        <xsl:when test="not(contains($text, $separator))">
            <swiftResponseReturnValue>
                <xsl:value-of select="$text"/>
            </swiftResponseReturnValue>
        </xsl:when>
        <xsl:otherwise>
            <swiftResponseReturnValue>
                <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
            </swiftResponseReturnValue>
            <xsl:call-template name="tokenize">
                <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                <xsl:with-param name="separator" select="$separator"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
  <!--<xsl:template match="soapenv:Envelope/soapenv:Body/xs:swiftResponseResponse/xs:swiftResponseReturn">-->
   <xsl:template match="xs:swiftResponseReturn">
  <swiftResponseReturn>
     <xsl:call-template name="tokenize">
         <xsl:with-param name="text" select="."/>
         <xsl:with-param name="separator" select="'|'"/>
     </xsl:call-template>
  </swiftResponseReturn>
</xsl:template>
</xsl:stylesheet>

我的输出是

     <swiftResponseReturn>
   <swiftResponseReturnValue>15617TS006140</swiftResponseReturnValue>
   <swiftResponseReturnValue>DC768736</swiftResponseReturnValue>
   <swiftResponseReturnValue>13321.49</swiftResponseReturnValue>
   <swiftResponseReturnValue>04-05-2017 15:13:03</swiftResponseReturnValue>
   <swiftResponseReturnValue>SWIFTINR</swiftResponseReturnValue>
   <swiftResponseReturnValue>NA</swiftResponseReturnValue>
   <swiftResponseReturnValue>FAIL-ACCOUNT UNAVAILABLE</swiftResponseReturnValue>
</swiftResponseReturn>

我希望我的预期输出应该是这样的

    <swiftResponseReturn>
   <swiftResponseReturnValue1>15617TS006140</swiftResponseReturnValue1>
   <swiftResponseReturnValue2>DC768736</swiftResponseReturnValue2>
   <swiftResponseReturnValue3>13321.49</swiftResponseReturnValue3>
   <swiftResponseReturnValue4>04-05-2017 15:13:03</swiftResponseReturnValue4>
   <swiftResponseReturnValue5>SWIFTINR</swiftResponseReturnValue5>
   <swiftResponseReturnValue6>NA</swiftResponseReturnValue6>
   <swiftResponseReturnValue7>FAIL-ACCOUNT UNAVAILABLE</swiftResponseReturnValue7>
</swiftResponseReturn>

可以帮助我在我的标签中添加 ids 或任何其他可以唯一标识我的列的值。请帮助我卡在这里一段时间。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    在元素名称中具有索引通常不被认为是一种好的 XML 设计,但如果您确定要在模板中添加参数并使用它,可以在元素名称中或在属性中使用它:

      <xsl:template name="tokenize">
        <xsl:param name="text"/>
        <xsl:param name="separator" />
        <xsl:param name="index" select="1"/>
        <xsl:choose>
            <xsl:when test="not(contains($text, $separator))">
                <swiftResponseReturnValue pos="{$index}">
                    <xsl:value-of select="$text"/>
                </swiftResponseReturnValue>
            </xsl:when>
            <xsl:otherwise>
                <swiftResponseReturnValue pos="{$index}">
                    <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
                </swiftResponseReturnValue>
                <xsl:call-template name="tokenize">
                    <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                    <xsl:with-param name="separator" select="$separator"/>
                    <xsl:with-param name="index" select="$index + 1"/>
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    

    如果要使用索引构造元素名称,请使用 &lt;xsl:element name="swiftResponseReturnValue{$index}"&gt;...&lt;/xsl:element&gt; 而不是 &lt;swiftResponseReturnValue pos="{$index}"&gt;

    【讨论】:

    • 实际上 pos 或 ids 不能满足我的要求。我只想在标签中添加值
    • 是他们的做法
    • 一旦你有了那个参数,你当然可以使用xsl:element构造一个元素名称,我已经在答案中添加了语法。
    【解决方案2】:

    首先,您为什么要这样做?它使处理结果更加困难。您应该使用描述其内容含义的元素名称。

    话虽如此,您可以向标记化模板添加第三个可选参数,默认填充为 1,并在递归调用标记化时向其添加 1。然后使用该参数构造标记名。类似:

    &lt;xsl:element name="'swiftResponseReturnValue'+$myThirdParameter"&gt;

    <xsl:template name="tokenize">
      <xsl:param name="text"/>
      <xsl:param name="separator" />
      <xsl:param name="index" select="1"/>
      <xsl:choose>
          <xsl:when test="not(contains($text, $separator))">
              <xsl:element name="'swiftResponseReturnValue'+$index">
                  <xsl:value-of select="$text"/>
              <xsl:element>
          </xsl:when>
          <xsl:otherwise>
              <swiftResponseReturnValue pos="{$index}">
                  <xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
              </swiftResponseReturnValue>
              <xsl:call-template name="tokenize">
                  <xsl:with-param name="text" select="substring-after($text, $separator)"/>
                  <xsl:with-param name="separator" select="$separator"/>
                  <xsl:with-param name="index" select="$index + 1"/>
              </xsl:call-template>
          </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    

    【讨论】:

    • acutually 它的要求,它不能用任何其他方式完成。你能添加工作代码,因为我是初学者
    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2014-03-30
    • 2014-12-05
    • 2021-12-15
    相关资源
    最近更新 更多