0

如何将以下/连续值参数合并到输出主卡 1234。

XML

<CustomFieldList>
<CustomField>
      <Name>PaymentCardType1</Name>
      <Value>Master Card</Value>
    </CustomField>
    <CustomField>
      <Name>CardDisplayNumber1</Name>
      <Value>1234</Value>
    </CustomField>
      Name>PaymentCardType2</Name>
      <Value>Gift Card</Value>
    </CustomField>
    <CustomField>
      <Name>CardDisplayNumber2</Name>
      <Value>6789</Value>
    </CustomField>
 </CustomFieldList>

XSLT

<xsl:for-each select="/cXML/Message/CustomFieldList">
      <xsl:for-each select="/cXML/Message/CustomFieldList/CustomField">
....
</xsl:for-each>
      </xsl:for-each>

可以创建任何自定义标签来存储付款类型和号码

 <payment1>Master Card 1234</payment1>
 <payment2>Gift Card 6789</payment2>
4

5 回答 5

1

请试一试:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:key name="kCardNumber" 
           match="CustomField[starts-with(Name, 'CardDisplayNumber')]"
           use="substring-after(Name, 'CardDisplayNumber')"/>

  <xsl:template match="/*">
    <r>
      <xsl:apply-templates 
         select=".//CustomField[starts-with(Name, 'PaymentCardType')]" />
    </r>
  </xsl:template>

  <xsl:template match="CustomField[starts-with(Name, 'PaymentCardType')]">
    <xsl:variable name="num" select="substring-after(Name, 'PaymentCardType')" />
    <xsl:element name="payment{$num}">
      <xsl:value-of select="concat(Value, ' ', key('kCardNumber', $num)/Value)"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

在您的示例 XML 上运行时(一旦问题得到解决),结果是:

<r>
  <payment1>Master Card 1234</payment1>
  <payment2>Gift Card 6789</payment2>
</r>
于 2013-05-14T06:14:02.780 回答
1

尝试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

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

    <xsl:template match="CustomFieldList">
        <xsl:for-each select="CustomField[starts-with(Name,'PaymentCardType')]">
            <xsl:variable name ="nr" select="substring-after(Name,'PaymentCardType')" />

            <xsl:element name="{concat('payment', $nr)}" >
                <xsl:value-of  select="Value"/>
                <xsl:text> </xsl:text>
                <xsl:value-of select="../CustomField[Name =concat('CardDisplayNumber', $nr)]/Value"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

输出:

<payment1>Master Card 1234</payment1>
<payment2>Gift Card 6789</payment2>
于 2013-05-14T05:22:12.360 回答
0

Hope you get the idea,

<xsl:value-of select="concat(.[./../Name = "PaymentCardType1"], .[./../Name = "CardDisplayNumber1"])" />
于 2013-05-14T04:43:05.320 回答
0

你应该使用类似的东西:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fb="http://www.facebook.com/2008/fbml">

  <xsl:strip-space elements="*"/>

  <xsl:template match="CustomFieldList">
  <xsl:for-each select="CustomField">
    <xsl:value-of select="Value"/><xsl:text> </xsl:text>
  </xsl:for-each>

</xsl:template>
</xsl:stylesheet>

输入xml:

<CustomFieldList>
  <CustomField>
    <Name>PaymentCardType1</Name>
    <Value>Master Card</Value>
  </CustomField>
  <CustomField>
    <Name>CardDisplayNumber1</Name>
    <Value>1234</Value>
  </CustomField>
  <CustomField>
  <Name>PaymentCardType2</Name>
  <Value>Gift Card</Value>
  </CustomField>
  <CustomField>
    <Name>CardDisplayNumber2</Name>
    <Value>6789</Value>
  </CustomField>
</CustomFieldList>

得到输出:

Master Card 1234 Gift Card 6789 
于 2013-05-14T04:35:38.053 回答
0

尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<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"
      xmlns:user="">

<xsl:output omit-xml-declaration="yes"/>

<xsl:template match="/">
<xsl:call-template name="ABC">
  <xsl:with-param name="i">1</xsl:with-param>
  <xsl:with-param name="count" >
    <xsl:value-of select="count(//CustomField)"/>
  </xsl:with-param>

</xsl:call-template>

</xsl:template>
<xsl:template name="ABC">
<xsl:param name="i"></xsl:param>
<xsl:param name="count"></xsl:param>
<xsl:if test="$i &lt;= $count">
  <xsl:value-of select="//CustomField[position()=$i]/Value"/>
  <xsl:text> </xsl:text>
  <xsl:call-template name="ABC">
    <xsl:with-param name="i" >
      <xsl:value-of select="$i+1"/>
    </xsl:with-param>
    <xsl:with-param name="count">
      <xsl:value-of select="$count"/>
    </xsl:with-param>
  </xsl:call-template>
</xsl:if>

于 2013-05-14T05:23:20.773 回答