开发者

Excluding Namespaces in XML results

开发者 https://www.devze.com 2023-03-23 16:12 出处:网络
I am trying to figure out how best I can process the XML file below so the resulting XML file excludes namespace declarations.

I am trying to figure out how best I can process the XML file below so the resulting XML file excludes namespace declarations.

XML Input

<?xml version="1.0" encoding="UTF-8"?>
<page xmlns:b="http://book.com/" xmlns:p="http://page.com/">
  <b:title>Book Title</b:title>
  <p:number>page001</p:number>
  <p:number>page002</p:number>
  <p:number>page001</p:number>
  <p:number>page002</p:number>
</page>

Current XSL File

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b="http://book.com/" 
xmlns:p="http://page.com/"
>

 <xsl:output method="xml" indent="yes" encoding="UTF-8" />

 <xsl:template match="resource">
   <xsl:apply-templates select="b:title" />
   <xsl:apply-templates select="p:number" />
 </xsl:template>

 <xsl:template match="b:title">
   <title exclude-result-prefixes="#all">
     <xsl:value-of select="." />
   </title>
 </xsl:template>

 <xsl:template match="p:number">
   <page exclude-result-prefixes="#all">
     <xsl:value-of select="." />
   </page>
 </xsl:template>

</xsl:stylesheet>

Current Output

  <title xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">Book Title</title>
  <page xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">page001</page>
  <page xmlns:b="http://book.com/" xmlns:p="http://page.com/" ex开发者_StackOverflowclude-result-prefixes="#all">page002</page>
  <page xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">page001</page>
  <page xmlns:b="http://book.com/" xmlns:p="http://page.com/" exclude-result-prefixes="#all">page002</page>

Desired Output

<?xml version="1.0" encoding="UTF-8"?>
  <title>Book Title</title>
  <page>page001</page>
  <page>page002</page>
  <page>page001</page>
  <page>page002</page>


This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="*">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="*[local-name()='number']">
  <page>
   <xsl:value-of select="."/>
  </page>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<page xmlns:b="http://book.com/" xmlns:p="http://page.com/">
    <b:title>Book Title</b:title>
    <p:number>page001</p:number>
    <p:number>page002</p:number>
    <p:number>page001</p:number>
    <p:number>page002</p:number>
</page>

produces the wanted, correct result:

<page>
    <title>Book Title</title>
    <page>page001</page>
    <page>page002</page>
    <page>page001</page>
    <page>page002</page>
</page>

Explanation:

  1. Use of the xsl:element instruction to create (not copy!) a new element with name that is the local-name() of the matched element.

  2. A template matching elements with local-name() number to "rename" them to page


Use the exclude-result-prefixes attribute on the xsl:stylesheet element.

In your case, something like:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:b="http://book.com/" 
xmlns:p="http://page.com/"
exclude-result-prefixes="b p"
>

 <xsl:output method="xml" indent="yes" encoding="UTF-8" />

 <xsl:template match="resource">
   <xsl:apply-templates select="b:title" />
   <xsl:apply-templates select="p:number" />
 </xsl:template>

 <xsl:template match="b:title">
   <title>       
     <xsl:value-of select="." />       
   </title>
 </xsl:template>

 <xsl:template match="p:number">       
   <page>
     <xsl:value-of select="." />       
   </page>
 </xsl:template>

</xsl:stylesheet>
0

精彩评论

暂无评论...
验证码 换一张
取 消