开发者

Last item in a node

I'm trying to find the last item in a node, so I can specify whether to specify a comma or not.

<?xml version="1.0开发者_Python百科" encoding="utf-16"?>
<?xml-stylesheet href="data.xsl" type="text/xsl"?>
<page generated="2011-04-19T19:22:21+00:00">
  <realm generated="2011-04-19T14:36:41-04:00" name="H-Bloodhoof">
    <item id="24033" name="Solid Star of Elune">
      <market>230000</market>
      <quantity>0</quantity>
      <reagentprice>39204</reagentprice>
    </item>
    <item id="5976" name="Guild Tabard">
      <market>60000</market>
      <quantity>1</quantity>
      <marketaverage>61162</marketaverage>
      <marketstddev>19735</marketstddev>
      <lastseen>2011-04-19T14:30:50-04:00</lastseen>
    </item>
    <item id="24036" name="Flashing Living Ruby">
      <market>1100000</market>
      <quantity>0</quantity>
      <reagentprice>99617</reagentprice>
    </item>
    <item id="23108" name="Timeless Shadow Draenite">
      <market>20000</market>
      <quantity>0</quantity>
      <marketaverage>20000</marketaverage>
      <marketstddev>0</marketstddev>
      <reagentprice>6500</reagentprice>
      <lastseen>2011-04-15T00:46:15-04:00</lastseen>
    </item>
  </realm>
</page>

Here's xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />

<xsl:template match="realm">
    <xsl:apply-templates >
        <xsl:with-param name="id" select="@id" />
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="item">

    <xsl:param name="id" />

    <xsl:text>addonTable.marketdata[</xsl:text><xsl:value-of select="@id" /><xsl:text>] = {</xsl:text>

        <xsl:text>["market"] =</xsl:text><xsl:value-of select="market" />
        <xsl:if test="not(following-sibling::*)"><xsl:text>,</xsl:text></xsl:if>

        <xsl:text>["quantity"] =</xsl:text><xsl:value-of select="quantity" />
        <xsl:if test="not(following-sibling::*)"><xsl:text>,</xsl:text></xsl:if>

        <xsl:text>["marketaverage"] =</xsl:text><xsl:value-of select="marketaverage" />
        <xsl:if test="not(following-sibling::*)"><xsl:text>,</xsl:text></xsl:if>

        <xsl:text>["marketstddev"] =</xsl:text><xsl:value-of select="marketstddev" />
        <xsl:if test="not(following-sibling::*)"><xsl:text>,</xsl:text></xsl:if>

        <xsl:text>["reagentprice"] =</xsl:text><xsl:value-of select="reagentprice" />
        <xsl:if test="not(following-sibling::*)"><xsl:text>,</xsl:text></xsl:if>

        <xsl:text>["lastseen"] =</xsl:text><xsl:value-of select="lastseen" />
        <xsl:if test="not(following-sibling::*)"><xsl:text>,</xsl:text></xsl:if>

    <xsl:text>}</xsl:text>

</xsl:template>

</xsl:stylesheet>

So right now it's checking for the last sibling of Item where I would like it do be the last element within the item.

addonTable.marketdata[24033] = {["market"] =230000["quantity"] =0["marketaverage"] =["marketstddev"] =["reagentprice"] =39204["lastseen"] =}

addonTable.marketdata[5976] = {["market"] =60000["quantity"] =1["marketaverage"] =61162["marketstddev"] =19735["reagentprice"] =["lastseen"] =2011-04-19T14:30:50-04:00}

addonTable.marketdata[24036] = {["market"] =1100000["quantity"] =0["marketaverage"] =["marketstddev"] =["reagentprice"] =99617["lastseen"] =}

addonTable.marketdata[23108] = {["market"] =20000,["quantity"] =0,["marketaverage"] =20000,["marketstddev"] =0,["reagentprice"] =6500,["lastseen"] =2011-04-15T00:46:15-04:00,}

It should look like this and it would be great if it doesn't print the empty values.

  addonTable.marketdata[24033] = {["market"] =230000,["quantity"] =0,["marketaverage"] =,["marketstddev"] =,["reagentprice"] =39204,["lastseen"] =}

  addonTable.marketdata[5976] = {["market"] =60000,["quantity"] =1,["marketaverage"] =61162,["marketstddev"] =19735,["reagentprice"] =,["lastseen"] =2011-04-19T14:30:50-04:00}

  addonTable.marketdata[24036] = {["market"] =1100000,["quantity"] =0,["marketaverage"] =,["marketstddev"] =,["reagentprice"] =99617,["lastseen"] =}

  addonTable.marketdata[23108] = {["market"] =20000,["quantity"] =0,["marketaverage"] =20000,["marketstddev"] =0,["reagentprice"] =6500,["lastseen"] =2011-04-15T00:46:15-04:00}

Thanks for looking!


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:strip-space elements="*"/>
    <xsl:output method="text"/>
    <xsl:template match="item">
        <xsl:value-of select="concat('addonTable.marketdata[',@id,'] = {')"/>
        <xsl:apply-templates/>
        <xsl:text>}&#xA;</xsl:text>
    </xsl:template>
    <xsl:template match="item/*">
        <xsl:if test="position()!=1">,</xsl:if>
        <xsl:value-of select="concat('[&quot;',name(),'&quot;] = ',.)"/>
    </xsl:template>
</xsl:stylesheet>

Output:

addonTable.marketdata[24033] = {["market"] = 230000,["quantity"] = 0,["reagentprice"] = 39204}
addonTable.marketdata[5976] = {["market"] = 60000,["quantity"] = 1,["marketaverage"] = 61162,["marketstddev"] = 19735,["lastseen"] = 2011-04-19T14:30:50-04:00}
addonTable.marketdata[24036] = {["market"] = 1100000,["quantity"] = 0,["reagentprice"] = 99617}
addonTable.marketdata[23108] = {["market"] = 20000,["quantity"] = 0,["marketaverage"] = 20000,["marketstddev"] = 0,["reagentprice"] = 6500,["lastseen"] = 2011-04-15T00:46:15-04:00}


<?xml version="1.0" encoding="ISO-8859-1"?>
  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" />

  <xsl:template match="realm">
     <xsl:apply-templates >  
         <xsl:with-param name="id" select="@id" />
      </xsl:apply-templates>
  </xsl:template>

 <xsl:template match="item">

     <xsl:param name="id" />

     <xsl:text>addonTable.marketdata[</xsl:text><xsl:value-of select="@id" /><xsl:text>] = {</xsl:text>

      <xsl:for-each select="*">                           
              <xsl:text>["</xsl:text><xsl:value-of select="local-name()"/><xsl:text>"] =</xsl:text><xsl:value-of select="text()"/><xsl:if test="position()!=last()"><xsl:text>,</xsl:text></xsl:if>                                           
      </xsl:for-each>                                                     

     <xsl:text>}</xsl:text>                                                                                                                                               
  </xsl:template>                                                                                                 </xsl:stylesheet>


I'd create a template for market|quantity|etc and move teh comma check into tha, it will also simplify what your are currently doing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜