如何使用XSLT替换XML文件中的文本

时间:2012-11-14 09:10:34

标签: xslt

我有一个XML文件和一个XSLT文件,如下所示

Check.xml

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog>

Check.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:for-each select="/">
<xsl:choose>
      <xsl:when test="country='USA'">
         <xsl:copy-of select="CHK"/>
      </xsl:when>
      <xsl:otherwise>
         <xsl:copy-of select="*"/>
      </xsl:otherwise>
      </xsl:choose>
 </xsl:template>
</xsl:stylesheet>

当我尝试转换它时,我得到以下错误

Error:XSLTProcessor::transformToXml() [<a href='xsltprocessor.transformtoxml'>xsltprocessor.transformtoxml</a>]: No stylesheet associated to this object

我在这里尝试做的是检查值是否为“USA”,如果是,则用“CHK”字符串替换USA。

我没有得到我出错的地方,或者我没有使用正确的语法。 我是XSLT的新手,刚刚开始使用它。任何帮助是极大的赞赏!!! 我期待的输出是

<catalog>
        <cd>
            <title>Empire Burlesque</title>
            <artist>Bob Dylan</artist>
            <country>**CHK**</country>
            <company>Columbia</company>
            <price>10.90</price>
            <year>1985</year>
        </cd>
        <cd>
            <title>Hide your heart</title>
            <artist>Bonnie Tyler</artist>
            <country>UK</country>
            <company>CBS Records</company>
            <price>9.90</price>
            <year>1988</year>
        </cd>
</catalog>

1 个答案:

答案 0 :(得分:3)

当这个XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="country[. = 'USA']">
    <country>**CHK**</country>
  </xsl:template>

</xsl:stylesheet>

...适用于提供的XML:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Edited by XMLSpy® -->
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

...生成所需的输出:

<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>**CHK**</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

<强>解释

  • 第一个模板是Identity Transform - 它的工作是按原样将源文档中的所有节点和属性输出到结果文档。
  • 第二个模板通过匹配任何值为“USA”的<country>元素来覆盖Identity Transform。在这种情况下,会创建一个新的<country>元素,并给出所需的值。

我建议您查看this link以获取一些出色的XSLT学习资源。