1

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>
1
  • There isn't a <CHK> element in your XML. Please update your question and give an example of what you'd like your output XML to look like. Commented Nov 14, 2012 at 9:15

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but iam still getting the same error pasted above. Please help :( This is the link where I am checking it. xslt.online-toolz.com/tools/xslt-transformation.php
Not sure what to tell you as I didn't create that online-toolz.com site. As it is, I just tested my solution there and got the correct result. Going forward, I recommend that you install a sensible XSLT parser on your own machine - start here: sagehill.net/docbookxsl/InstallingAProcessor.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.