English 中文(简体)
I ADD如何利用XSLT对XML的根干进行污染?
原标题:How do I ADD an Attribute to the Root Element in XML using XSLT?

我想把“FOO”这一根基同起来,并作转变(加上一个版本的属性),使之留下来。 我迄今所期望的是:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://schemas.foo.com/fooNameSpace">

<xsl:template match="//FOO">
    <xsl:choose>
      <xsl:when test="@version">
        <xsl:apply-templates select="node()|@*" />
      </xsl:when>
      <xsl:otherwise>
        <FOO>
         <xsl:attribute name="version">1</xsl:attribute>
         <xsl:apply-templates select="node()|@*" />
        </FOO>
      </xsl:otherwise>
    </xsl:choose>
 </xsl:template>

然而,这并没有进行任何转变。 它甚至没有发现该元素。 因此,我需要增加名称空间,以便发挥作用:

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fd="http://schemas.foo.com/fooNameSpace">

 <xsl:template match="//fd:FOO">
 …

但是,这为联络处的构成部分和其他要素提供了一个名称空间:

<FOO xmlns:fd="http://schemas.foo.com/fooNameSpace" version="1" id="fooid">
<BAR xmlns="http://schemas.foo.com/fooNameSpace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  • Is there a way to say that the element is using the default namespace?
  • Can we match and add elements in the default name space?

The original XML:

  <?xml version="1.0" encoding="UTF-8"?>
  <FOO xmlns="http://schemas.foo.com/fooNameSpace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <BAR>
        <Attribute name="HEIGHT">2067</Attribute>
      </BAR>
  </FOO>
问题回答

这对我使用xsltproc/libxslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="http://schemas.foo.com/fooNameSpace">
  <xsl:template match="/ns:FOO">
    <xsl:copy>
      <xsl:if test="not(@version)">
        <xsl:attribute name="version">1</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

它生产:

<?xml version="1.0"?>
<FOO xmlns="http://schemas.foo.com/fooNameSpace" version="1">
    <BAR>
        <Attribute>2067</Attribute>
    </BAR>
</FOO>

www.un.org/Depts/DGACM/index_spanish.htm 这里,一种真正符合《国际空间法学会精神的解决办法:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fd="http://schemas.foo.com/fooNameSpace"
    >
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*" name="identity">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/*[self::fd:FOO and not(@version)]">
  <xsl:copy>
    <xsl:attribute name="version">1</xsl:attribute>

    <xsl:call-template name="identity"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

www.un.org/Depts/DGACM/index_spanish.htm 如果在提供的XML文件上适用这一转变,就会产生正确的结果:

<FOO xmlns="http://schemas.foo.com/fooNameSpace"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 version="1">
   <FOO>
      <BAR>
         <Attribute name="HEIGHT">2067</Attribute>
      </BAR>
   </FOO>
</FOO>




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签