遍历XSLT中的xml节点

时间:2014-10-10 04:16:32

标签: xslt-1.0

在xslt 1.0中。我正在调用一个java函数getTierByBundleId,它返回xml以下。我想使用每个层迭代每个层。我怎么能这样做

<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>

这里当我尝试做xsl:对于每个有xml的变量我都会遇到编译错误。如何从变量$ varTierList

访问每个层
<xsl:for-each select="$varTierList/TierList/Tier">
<TierId><xsl:for each"Tierid"/>
</xsl:for-each

下面是生成上述TierList xml

的xslt
<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>

1 个答案:

答案 0 :(得分:0)

希望这有帮助, 这是完整的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>