节点中的最后一项

发布于 2024-11-03 03:39:11 字数 4815 浏览 1 评论 0原文

我正在尝试查找节点中的最后一项,因此我可以指定是否指定逗号。

<?xml version="1.0" 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>

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

所以现在它正在检查 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,}

它应该看起来像这样,如果它不打印空值那就太好了。

  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}

感谢您的浏览!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自演自醉 2024-11-10 03:39:11

此样式表:

<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>}
</xsl:text>
    </xsl:template>
    <xsl:template match="item/*">
        <xsl:if test="position()!=1">,</xsl:if>
        <xsl:value-of select="concat('["',name(),'"] = ',.)"/>
    </xsl:template>
</xsl:stylesheet>

输出:

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] = {["市场"] = 1100000,["数量"] = 0,["试剂价格"] = 99617}
addonTable.marketdata[23108] = {["市场"] = 20000,["数量"] = 0,["市场平均"] = 20000,["市场stddev"] = 0,["试剂价格"] = 6500 ,["lastseen"] = 2011-04-15T00:46:15-04:00}

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>}
</xsl:text>
    </xsl:template>
    <xsl:template match="item/*">
        <xsl:if test="position()!=1">,</xsl:if>
        <xsl:value-of select="concat('["',name(),'"] = ',.)"/>
    </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}

四叶草在未来唯美盛开 2024-11-10 03:39:11
<?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>
<?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>
待"谢繁草 2024-11-10 03:39:11

我会为 market|quantity|etc 创建一个模板,并将逗号检查移到 tha 中,它也会简化您当前正在做的事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文