How to replace a text in XML file using XSLT

2019-08-13 22:40发布

I have a XML file and a XSLT file as below

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>

When I try to transform it i get the below error

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

What Iam trying to do here is that check if the value is "USA" and if yes the replace USA with "CHK" String.

Iam not getting where I am going wrong or if Iam not using the correct syntax's. I am a newbie to XSLT and just got started with this. Any help is greatly appreciated!!! The output I am expecting is

<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>

标签: xslt
1条回答
你好瞎i
2楼-- · 2019-08-13 23:26

When this 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>

...is applied to the provided 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>

...the desired output is produced:

<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>

Explanation:

  • The first template is the Identity Transform - its job is to output all nodes and attributes from the source document to the result document as-is.
  • The second template overrides the Identity Transform by matching any <country> element whose value is "USA". In this case, a new <country> element is created and given the desired value.

I recommend you look at this link for some great XSLT learning resources.

查看更多
登录 后发表回答