创建XSL以仅获取某些节点



我希望只是一个快速的,我正在尝试为下面的xml创建一个XSL。

<?xml version="1.0" encoding="UTF-8"?>
<CurrentUsage xmlns="http://au.com.amnet.memberutils/"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <OtherLimit>0</OtherLimit>
   <PeerLimit>0</PeerLimit>
   <PeriodEnd>2013-06-15T00:00:00</PeriodEnd>
   <PeriodStart>2013-05-15T00:00:00</PeriodStart>
   <PlanName>ADSL 2+ Enabled 120G/180G - $69.00</PlanName>
   <RateLimited>0</RateLimited>
   <otherInGB>9.51</otherInGB>
   <otherOutGB>2.06</otherOutGB>
   <peerInGB>0.12</peerInGB>
   <peerOutGB>0.02</peerOutGB>
</CurrentUsage>

我想从中得到的只是……的内容。

  • otherInGB
  • outherOutGB
  • peerInGb
  • peerOutGb
  • 这4个值的总和(如果可能的话,只使用XSL)。

我已经尝试了许多不同的XSL文档(我不是专家),但是我被卡住了,因为在我看来没有真正的父节点。这种说法正确吗?

<?xml version="1.0" encoding="iso-8859-1"?>
   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
         <xsl:for-each select="CurrentUsage">
    <xsl:value-of select="otherInGB"/>
    <br></br>
    <xsl:value-of select="otherOutGB"/>
    <br></br>
    <xsl:value-of select="peerInGB"/>
    <br></br>
    <xsl:value-of select="peerOutGB"/>
    <br></br>
                  </xsl:for-each>
     </xsl:template>
   </xsl:stylesheet>

我知道这是错误的,因为它不返回任何东西,我知道for-each选择实际上导致了问题。无论如何,任何帮助或指点将不胜感激。

欢呼,

特伦特

这里的主要问题是xml文件有一个名称空间。因此,需要将名称空间(带前缀)添加到xslt中。试试这样做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:mu="http://au.com.amnet.memberutils/"
                exclude-result-prefixes="mu">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="mu:CurrentUsage">
        <xsl:value-of select="mu:otherInGB"/>
        <br></br>
        <xsl:value-of select="mu:otherOutGB"/>
        <br></br>
        <xsl:value-of select="mu:peerInGB"/>
        <br></br>
        <xsl:value-of select="mu:peerOutGB"/>
        <br></br>
    </xsl:template>
    <xsl:template match="/">
        <xsl:apply-templates select="mu:CurrentUsage " />
    </xsl:template>
</xsl:stylesheet>

将生成以下输出:

<?xml version="1.0"?>
9.51<br/>2.06<br/>0.12<br/>0.02<br/>

最新更新