0

次の機能が期待どおりに機能しないのはなぜですか?

<root xmlns:ns0="xmlns" 
      ns0:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:ns1="xsi" 
      ns1:schemaLocation="[some schema location]" />

基本的に、私はこれを持たないxmlファイルにschemaLocationを追加しようとしています:-

<xsl:template match="/s:*">
  <xsl:element name="{local-name()}" namespace="some other namespace">
    <xsl:attribute namespace="xmlns" name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>
    <xsl:attribute namespace="xsi" name="schemaLocation">[some-loc]</xsl-attribute>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

Xalan-Cは上記のxmlを提供します。

私が取得しようとしているのは次のようなものです:-

<root xmlns:ns0="http://www.w3.org/2001/XMLSchema-instance"
      ns0:schemaLocation="[some schema location]" />
4

2 に答える 2

3

必要<xsl:attribute name="xsi:schemaLocation">[some-loc]</xsl:attribute>です。

編集(完全な例を追加):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
   <xsl:template match="root">
      <xsl:element name="{local-name()}" namespace="some other namespace">
         <!--<xsl:attribute namespace="xmlns" name="xsi">http://www.w3.org/2001/XMLSchema-instance</xsl:attribute>-->
         <xsl:attribute name="xsi:schemaLocation">[some-loc]</xsl:attribute>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

正確に何をしているかによっては、 ;<xsl:copy>の代わりに要素をコピーするために使用できる可能性があります。<xsl:element name="{local-name()}"/>やや簡単です。

于 2012-09-24T19:23:16.227 に答える
3

私があなたの質問を解釈し、あなたが実際に何を望んでいたかを推測しているので、問題は、あなたが名前空間プレフィックスnamespace="..."になることを意図しているが、実際には名前空間URIであることになっているという値を入れていることです。

Thus the code you probably want is just one <xsl:attribute> element:

<xsl:template match="/s:*">
  <xsl:element name="{local-name()}" namespace="some other namespace">
    <xsl:attribute namespace="http://www.w3.org/2001/XMLSchema-instance" 
        name="xsi:schemaLocation">[some-loc]</xsl-attribute>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:element>
</xsl:template>

Then the XSLT processor will output the specified schemaLocation attribute in the right namespace, and will also emit a namespace declaration for its prefix where needed (and its prefix may be xsi:, but doesn't have to be).

Part of the problem here is that people throw around the word "namespace" (mentally or to others) without being clear about what they mean. A namespace is not a namespace prefix, nor is it really a namespace URI. A namespace is globally identified by its URI, and locally identified by its prefix.

If the XSLT spec had named this attribute namespace-uri="..." instead of namespace="...", fewer people would fall into this trap. You can also protect yourself by asking whether someone who says "namespace" really means "namespace URI", "namespace prefix", "namespace declaration", etc. etc.

于 2012-09-24T19:23:50.900 に答える