Sakthivel
Sakthivel

Reputation: 666

XML is not producing in a right way

I am working on this snippets, but, i am getting the expected output.

Here is my input

<?xml version="1.0" encoding="UTF-8" ?>
<GovTalkMessage xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader">
  <EnvelopeVersion>1.0</EnvelopeVersion>
  <GovTalkDetails>
    <Keys/>
  </GovTalkDetails>
  <Body>
    <NameSearch>
      <ContinuationKey>fcb844eELdiGt/AO3sMH2IGP8Amoxy+wewviAdon</ContinuationKey>
      <RegressionKey>fcb844eJyt0ttO20moxy+wewviAdon</RegressionKey>
      <SearchRows>100</SearchRows>
      <CoSearchItem>
        <CompanyName>WILLIAM ROSE LTD</CompanyName>
        <CompanyNumber>07905646</CompanyNumber>
        <DataSet>LIVE</DataSet>
        <CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
        <CompanyDate></CompanyDate>
      </CoSearchItem>
    </NameSearch>
  </Body>
</GovTalkMessage>

And, here is my xslt

<?xml version="1.0"?>
<xsl:stylesheet xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:template match="GovTalkMessage/Body/NameSearch/CoSearchItem">
    <xsl:copy-of select="."/>
  </xsl:template>

</xsl:stylesheet>

My expected output is

<CoSearchItem>
  <CompanyName>WILLIAM ROSE LTD</CompanyName>
  <CompanyNumber>07905646</CompanyNumber>
  <DataSet>LIVE</DataSet>
  <CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
  <CompanyDate></CompanyDate>
</CoSearchItem>

Please someone help where is the problem.

Upvotes: 2

Views: 110

Answers (3)

Sakthivel
Sakthivel

Reputation: 666

I have used the following to achieve my desired output

<xsl:template match="/*[local-name()='GovTalkMessage']/*[local-name()='Body']/*[local-name()='NameSearch']/*[local-name()='CoSearchItem']">
<xsl:copy-of select="."/>
</xsl:template>

and, it works. Thanks for all.

Upvotes: 0

Daniel Haley
Daniel Haley

Reputation: 52888

Since you're using XSLT 2.0, you can also use the xpath-default-namespace attribute. That way you don't have to use a prefix in your xpaths.

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader" 
    xpath-default-namespace="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader" 
    version="2.0">

    <xsl:template match="/">
        <xsl:copy-of select="GovTalkMessage/Body/NameSearch/CoSearchItem"/>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Markus Jarderot
Markus Jarderot

Reputation: 89231

The first template that is applied is for the root node, and you need to specify namespaces if they are used at all.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:g="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="/">
    <xsl:copy-of select="/g:GovTalkMessage/g:Body/g:NameSearch/g:CoSearchItem"/>
  </xsl:template>

</xsl:stylesheet>

<xsl:output/> and <xsl:strip-space/> are just to fix the indentation.

Output:

<?xml version="1.0" encoding="UTF-8"?>
<CoSearchItem xmlns="http://www.govtalk.gov.uk/schemas/govtalk/govtalkheader">
   <CompanyName>WILLIAM ROSE LTD</CompanyName>
   <CompanyNumber>07905646</CompanyNumber>
   <DataSet>LIVE</DataSet>
   <CompanyIndexStatus>DISSOLVED</CompanyIndexStatus>
   <CompanyDate/>
</CoSearchItem>

Upvotes: 1

Related Questions