【问题标题】:remove "$" in xml with stylesheet使用样式表删除 xml 中的“$”
【发布时间】:2016-06-17 04:21:07
【问题描述】:

我有各种方法可以删除“statValue1”节点下的“$”,但没有运气。有人可以帮我设计样式表吗?

如果您在下面看到一些源 XML,我需要从所有 statTitle1 中删除 $

<?xml-stylesheet type="text/xsl" href="file:///C:/Users/davidhe/Documents/Altova/XMLSpy2016/Examples/stats.xml"?>
<tours>
    <tour tourCodeLC="s" tourCodeUC="S" tourName="Group!A>
        <years>
            <year year="2016">
                <lastTrnProc trnName="Through Week Ending: 02/29/2016" permNum="000" trnNum="032" endDate="Feb 28, 2016"/>
                <stats>
                    <stat statID="109" cat="1" rndOrEvt="Events">
                        <statName>Money List</statName>
                        <explanation>
The total official money a player has earned year-to-date. (109)
</explanation>
                        <tourAvg>$51,906</tourAvg>
                        <statTitles numOfStatTitles="3">
                            <statTitle1>Money</statTitle1>
                            <statTitle2/>
                            <statTitle3>YTD Victories</statTitle3>
                            <statTitle4/>
                            <statTitle5>Money Behind Lead</statTitle5>
                        </statTitles>
                        <details>
                            <plr plrNum="01666">
                                <plrName>
                                    <last>Smith</last>
                                    <first>Brad</first>
                                    <middle/>
                                    <nickname/>
                                </plrName>
                                <statValues>
                                    <rndEvents>3</rndEvents>
                                    <statValue1>
                                        $333,850</statValue1>
                                    <statValue2/>
                                    <statValue3>1</statValue3>
                                    <statValue4/>
                                    <statValue5/>
                                </statValues>
                                <curRank tied="">1</curRank>
                                <prevRank tied="">1</prevRank>
                            </plr>

样式表

<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform">
  <xsl:output encoding="iso-8859-1" method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="translate(//tours/tour/years/year/stats/stat/details/plr[1]/statValues/st‌​atValue1, '*\%!@$&amp;', '')"/>
</xsl:stylesheet>

【问题讨论】:

  • 使用starts-with()substring() 的组合。请记住,xslt 中的第一个字符的索引为 1 而不是 0。
  • 这就是我所拥有的......
  • w3.org/1999/XSL/Transform">

标签: xml xslt translate


【解决方案1】:

使用

<xsl:template match="statValues/st‌​atValue1">
  <xsl:copy>
    <xsl:value-of select="translate(., '$', '')"/>
  </xsl:copy>
</xsl:template>

而不是您拥有的第二个模板。

【讨论】:

  • 你应该改用match="statValues/*"
  • @MatthewWhited,你说得对,我在评论中迷路了,选择了错误的模式,现在更正了。
  • 也许我有这个错误,但这是我所拥有的,但它仍然不起作用。
  • w3.org/1999/XSL/Transform">
  • Matt,感谢您的帮助...看起来我快到了,但新样式表正在删除“statValues”节点...
【解决方案2】:

由于您为子元素使用不同的名称,因此您需要更通用一些。

<xsl:template match="statValues">
  <xsl:for-each select="*" >
  <xsl:copy>
    <xsl:value-of select="translate(., '$', '')"/>
  </xsl:copy>
  </xsl:for-each>
</xsl:template>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多