在任何地方检索全局XSLT变量

时间:2013-05-14 04:30:44

标签: xml xslt

如何将以下/连续的值参数合并到输出主卡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>

5 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分: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>

得到ouptut:

Master Card 1234 Gift Card 6789 

答案 3 :(得分:0)

希望你明白这个想法,

<xsl:value-of select="concat(.[./../Name = "PaymentCardType1"], .[./../Name = "CardDisplayNumber1"])" />

答案 4 :(得分: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>