在XSLT中遍历xml节点



xslt 1.0中的With。我正在调用一个java函数getTierByBundleId,该函数返回以下xml。我想使用for each遍历每个层。我该怎么做

<xsl:variable name="varTierList" select="testclass:getTierByBundleId($bid,$ApplicationId)"/>
Xml returned by function getTierByBundleId
<TierList>
<Tier>
    <TierId>1</TierId>
    <Name>test</Name>
    <Type>2</Type>
    <Price>10</Price>
</Tier>
<Tier>
    <TierId>2</TierId>
    <Name>test</Name>
    <Type>3</Type>
    <Price>11</Price>
</Tier>
</TierList>

在这里,当我试图对包含xml的变量执行xsl:for-each时,我会遇到编译错误。如何从变量$varTierList 访问每个层

<xsl:for-each select="$varTierList/TierList/Tier">
<TierId><xsl:for each"Tierid"/>
</xsl:for-each

下面是xslt,它正在生成上面的TierList xml

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:foxtelclass="test.GetOrderQuote" exclude-result-prefixes="foxtelclass" extension-element-prefixes="testclass">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
    <xsl:if test="count(/ROWSET/ROW) &gt; 0">
    <TierList>
        <xsl:for-each select="/ROWSET/ROW">
            <Tier>
                <TierId><xsl:value-of select="TIER_ID" /></TierId>
                <Name><xsl:value-of select="NAME" /></Name>
                <Type><xsl:value-of select="TIER_TYPE" /></Type>
                <Price><xsl:value-of select="PRICE" /></Price>
            </Tier>
        </xsl:for-each>
    </TierList>
    </xsl:if>
</xsl:template>
</xsl:stylesheet>
<xsl:for-each select="$varTierList/TierList/Tier"> is throwing the error message
Here is the full xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"       
 xmlns:testclass="test.GetOrderQuote"    
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="testclass" extension-
element-prefixes="testclass">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="ApplicationId"/>
<xsl:template match="/">
    <ServiceList>
        <xsl:variable name="unique-list" select="//ROWSET/ROW/SERVICE_ID
 [not(.=following::SERVICE_ID)]"/>
        <xsl:for-each select="$unique-list">
            <xsl:if test=".!=0">
                <Service>
                    <xsl:variable name="Sid" select="."/>
                    <ServiceInternalId>
                        <xsl:value-of select="."/>
                    </ServiceInternalId>
                    <BundleList>
                        <xsl:variable name="unique-
bundle" select="//SERVICE_ID/../BUNDLE_ID[not(.=following::BUNDLE_ID)]"/>
                        <xsl:for-each
select="$unique-bundle">
                            <xsl:variable 
name="bid" select="."/>
                            <xsl:if 
test="count(/ROWSET/ROW/SERVICE_ID[text()=$Sid]/../BUNDLE_ID[text()=$bid])&gt; 0">
                                <Bundle>
                                    <Bundle_Id>
                                        <xsl:value-of select="$bid"/>
                                    </Bundle_Id>
                                    <Type>
                                        <xsl:value-of select="../BUNDLE_TYPE"/>
                                    </Type>
                                    <Name>
                                        <xsl:value-of select="../BUNDLE_NAME"/>
                                    </Name>
                                    <Price>
                                        <xsl:value-of select="../BUNDLE_PRICE"/>
                                    </Price>
                                    <xsl:variable name="varTierList" select="testclass:getTierByBundleId
  ($bid,$ApplicationId)"/>
                                    <test>
                                        <xsl:value-of select="$varTierList"/>
                                    </test>
                                    <xsl:for-each select="$varTierList/TierList/Tier"><!--line causing error-->
                                        <TierId>
                                            <xsl:value-of select="TierId"/>
                                        </TierId>
                                    </xsl:for-each>
                                </Bundle>
                            </xsl:if>
                        </xsl:for-each>
                    </BundleList>
                </Service>
            </xsl:if>
        </xsl:for-each>
    </ServiceList>
</xsl:template>
</xsl:stylesheet>
Below is the text from test node which what is xml variable varTierList contains
<test>&lt;TierList&gt;&#13;
&lt;Tier&gt;&#13;
&lt;TierId&gt;109&lt;/TierId&gt;&#13;
&lt;Name&gt;Boxes&lt;/Name&gt;&#13;
&lt;Type&gt;10&lt;/Type&gt;&#13;
&lt;Price&gt;10&lt;/Price&gt;&#13;
&lt;/Tier&gt;&#13;
&lt;/TierList&gt;&#13;
</test>
Please find below input xml

<ROWSET> <ROW> <SET_ID>0</SET_ID> <SET_DATE>2014-11-09 00:00:00.0</SET_DATE> <BUNDLE_NAME>Test</BUNDLE_NAME> <BUNDLE_ID>131</BUNDLE_ID> <BUNDLE_PRICE>30</BUNDLE_PRICE> <BUNDLE_TYPE>3</BUNDLE_TYPE> <BUNDLECOMPONENT_LIST>101100</BUNDLECOMPONENT_LIST> <PACKAGE>10015</PACKAGE> <PACKAGE_TYPE>5</PACKAGE_TYPE> <COMPONENT>101100</COMPONENT> <PRODUCT_DESC>World Movies</PRODUCT_DESC> <RATE_AMOUNT>10</RATE_AMOUNT> <DISCOUNT_AMOUNT>0</DISCOUNT_AMOUNT> <SERVICE_ID>98683812</SERVICE_ID> <CHARGE_TYPE>RC</CHARGE_TYPE> <GUID_TOKEN>053944D794856E3FE0540010E00D30B8</GUID_TOKEN> <NRC_LINE_ID>0</NRC_LINE_ID> <TIERID>11</TIERID> </ROW> <ROW> <SET_ID>0</SET_ID> <SET_DATE>2014-11-09 00:00:00.0</SET_DATE> <BUNDLE_NAME>Optional test</BUNDLE_NAME> <BUNDLE_ID>131</BUNDLE_ID> <BUNDLE_PRICE>30</BUNDLE_PRICE> <BUNDLE_TYPE>3</BUNDLE_TYPE> <BUNDLECOMPONENT_LIST>101100</BUNDLECOMPONENT_LIST> <PACKAGE>10015</PACKAGE> <PACKAGE_TYPE>5</PACKAGE_TYPE> <COMPONENT>101103</COMPONENT> <PRODUCT_DESC>RAI International</PRODUCT_DESC> <RATE_AMOUNT>20</RATE_AMOUNT> <DISCOUNT_AMOUNT>0</DISCOUNT_AMOUNT> <SERVICE_ID>98683812</SERVICE_ID> <CHARGE_TYPE>RC</CHARGE_TYPE> <GUID_TOKEN>053944D794856E3FE0540010E00D30B8</GUID_TOKEN> <NRC_LINE_ID>0</NRC_LINE_ID> <TIERID>14</TIERID> </ROW> </ROWSET>

希望这能有所帮助,这是完整的xslt,在我的端没有显示任何编译错误

      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:testclass="test.GetOrderQuote" xmlns:ext="http://exslt.org/common" exclude-result-prefixes="testclass">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:param name="ApplicationId"/>
<xsl:template match="/">
<ServiceList>
  <xsl:variable name="unique-list" select="//ROWSET/ROW/SERVICE_ID[not(.=following::SERVICE_ID)]"/>
  <xsl:for-each select="$unique-list">
    <xsl:if test=".!=0">
      <Service>
        <xsl:variable name="Sid" select="."/>
        <ServiceInternalId>
          <xsl:value-of select="."/>
        </ServiceInternalId>
        <BundleList>
          <xsl:variable name="unique-bundle" select="//SERVICE_ID/../BUNDLE_ID[not(.=following::BUNDLE_ID)]"/>
          <xsl:for-each select="$unique-bundle">
            <xsl:variable name="bid" select="."/>
            <xsl:if test="count(/ROWSET/ROW/SERVICE_ID[text()=$Sid]/../BUNDLE_ID[text()=$bid])&gt; 0">
              <Bundle>
                <Bundle_Id>
                  <xsl:value-of select="$bid"/>
                </Bundle_Id>
                <Type>
                  <xsl:value-of select="../BUNDLE_TYPE"/>
                </Type>
                <Name>
                  <xsl:value-of select="../BUNDLE_NAME"/>
                </Name>
                <Price>
                  <xsl:value-of select="../BUNDLE_PRICE"/>
                </Price>
                <xsl:variable name="varTierList" select="testclass:getTierByBundleId($bid,$ApplicationId)"/>
                <test>
                  <xsl:value-of select="$varTierList"/>
                </test>
                <xsl:for-each select="$varTierList/TierList/Tier">
                  <TierId>
                    <xsl:value-of select="TierId"/>
                  </TierId>
                </xsl:for-each>
              </Bundle>
            </xsl:if>
          </xsl:for-each>
        </BundleList>
      </Service>
    </xsl:if>
  </xsl:for-each>
</ServiceList>
</xsl:template>
</xsl:stylesheet>

相关内容

  • 没有找到相关文章

最新更新