0

我希望只是一个快速的,我正在尝试为以下 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>

我想从中得到的只是...的内容

  • 其他InGB
  • 外出GB
  • 对等InGb
  • 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 选择实际上是导致问题的原因。无论如何,任何帮助或指针将不胜感激。

干杯,

特伦特

4

1 回答 1

1

您的主要问题是您的 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/>
于 2013-05-23T11:56:13.133 回答