0

I am facing an issue while transforming an input xml to oputput xml through xslt 2.0. Following is the input xml

<Heard sequence_id="10363284">
   <doctype>News</doctype>
   <Banner>--Alert--</Banner>
   <PCategory>WW</PCategory>
   <Topic>XX,YY,ZZ</Topic>
   <type>RealTime</type>
   <headline>xxxxxxxxxxxxxxxxxx</headline>
   <TextBody>xxxxxxxxx</TextBody>
   <headline_datetime>2014-09-09T10:51:27-04:00</headline_datetime>
   <service_line>ABC</service_line>
   <page_num>123</page_num>
</Heard>

The output expected should be

<mgh:message xsi:schemaLocation="http://comp.com/prismPlus-XSD HeardsContent.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:prism="http://prismstandard.org/namespaces/basic/2.0" xmlns:dc="http://purl.org/dc/elements/1.1" xmlns:prl="http://prismstandard.org/namespaces/prl/2.0" xmlns:mgh="http://comp.com/prismPlus-XSD">           
    <mgh:article>       
        <mgh:head>
            <dc:identifier>10363284</dc:identifier>
            <dc:title>xxxxxxxxxxxxxxxxxx</dc:title>
            <dc:publisher>Comp</dc:publisher>
            <dc:subject>--Alert--</dc:subject>
            <prism:publicationDate>09/08/2014</prism:publicationDate>
            <prism:subsection1>News</prism:subsection1>
            <prism:keyword>XX,YY,ZZ</prism:keyword>
            <mgh:category>WW</mgh:category>
            <mgh:serviceLine>ABC</mgh:serviceLine>
            <mgh:pageNumber>123</mgh:pageNumber>
        </mgh:head>
        <mgh:contentFeatureBody>
            <body>
                xxxxxxxxxxxxxxxxxxxx
            </body>
        </mgh:newsFeatureBody>                  
    </mgh:article>    
</mgh:message>

In the xslt 2.0, how do I add multiple namespaces like dc, prism, mgh? Please let me know if you need additional information as I am fairly new to SO and would highly appreciate if you can help me out.

2 Answers 2

2

If you're creating the mgh:message as a literal result element then provided those namespace declarations are in scope at the appropriate place it should just work:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:prism="http://prismstandard.org/namespaces/basic/2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1"
    xmlns:prl="http://prismstandard.org/namespaces/prl/2.0"
    xmlns:mgh="http://comp.com/prismPlus-XSD">

  <xsl:template match="/">
    <mgh:message xsi:schemaLocation="http://comp.com/prismPlus-XSD HeardsContent.xsd">
      <!-- etc -->

since literal result elements preserve the namespaces that are in scope at that point in the stylesheet.

You could of course just put the declarations on the mgh:message element itself rather than on the xsl:stylesheet, but then if you're creating the dc:* elements in another template you'd have to repeat the xmlns:dc there as well. Overall, I find it easier to put all the declarations at the root unless you have specific reasons not to.

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

2 Comments

Thanks for your reply. Although I tried your approach by adding all the namespace declarations in the root, still it gives me the following error when trying to perform the transformation. Namespace for prefix 'dc' has not been declared
@PawanSaha is it complaining about the stylesheet or about the input XML document? Do you maybe have <dc:something> elements in the input without an xmlns:dc declaration?
0

There's really no special secret to projecting namespaces and aliases into output documents - as with other xml documents, declare them in the root element, and then use the aliases. AFAIK the schemaLocation will need to be manually mapped onto the root element as an attribute. Here's a basic skeleton template (note that I've skipped most of the elements)

<xsl:stylesheet version="2.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:prism="http://prismstandard.org/namespaces/basic/2.0" 
                xmlns:dc="http://purl.org/dc/elements/1.1" 
                xmlns:prl="http://prismstandard.org/namespaces/prl/2.0" 
                xmlns:mgh="http://comp.com/prismPlus-XSD">

    <xsl:template match="/Heard">
    <mgh:message> <!--OR of course <msh xmlns="...">-->
       <xsl:attribute name="schemaLocation" 
                      namespace="http://www.w3.org/2001/XMLSchema-instance"
                      >http://comp.com/prismPlus-XSD HeardsContent.xsd</xsl:attribute>
      <mgh:article>
        <mgh:head>
          <dc:identifier>
            <xsl:value-of select="some/path/to/value"></xsl:value-of>
          </dc:identifier>
          <prism:keyword>
            <xsl:value-of select="Topic"></xsl:value-of>
          </prism:keyword>
        </mgh:head>
        <mgh:contentFeatureBody>
          <body> <!--Note, global namespace so no alias or xmlns-->
            <xsl:value-of select="TextBody"></xsl:value-of>
          </body>
        </mgh:contentFeatureBody>
      </mgh:article>
    </mgh:message>
  </xsl:template>
</xsl:stylesheet>

This produces the following output xml:

<?xml version="1.0" encoding="utf-8"?>
<mgh:message xsi:schemaLocation="http://comp.com/prismPlus-XSD HeardsContent.xsd" 
             xmlns:mgh="http://comp.com/prismPlus-XSD" 
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
             xmlns:prism="http://prismstandard.org/namespaces/basic/2.0" 
             xmlns:dc="http://purl.org/dc/elements/1.1" 
             xmlns:prl="http://prismstandard.org/namespaces/prl/2.0">
  <mgh:article>
    <mgh:head>
      <dc:identifier></dc:identifier>
      <prism:keyword>XX,YY,ZZ</prism:keyword>
    </mgh:head>
    <mgh:contentFeatureBody>
      <body>xxxxxxxxx</body>
    </mgh:contentFeatureBody>
  </mgh:article>
</mgh:message>

Comments

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.