如何更换使用XSLT在XML文件中的文本(How to replace a text in XML

2019-10-17 09:32发布

我有一个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>

Answer 1:

当这个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 -它的工作是输出的所有节点,并从源文件的属性到结果文档原样。
  • 第二模板覆盖身份通过匹配任何变换<country>元素,其值是“USA”。 在这种情况下,一个新的<country>元件被创建,并给予所需的值。

我建议你看一下这个链接一些伟大的XSLT学习资源。



文章来源: How to replace a text in XML file using XSLT
标签: xslt