Posso specificare uno spazio dei nomi predefinito nel mio foglio di stile XSL per ogni elemento creato?

StackOverflow https://stackoverflow.com/questions/1404756

  •  05-07-2019
  •  | 
  •  

Domanda

Sto usando .NET per trasformare XML da un DataSet nel formato sitemap . Ecco dove sono adesso. Come puoi vedere, creo l'elemento radice con lo spazio dei nomi corretto. Ho notato che se creavo nodi secondari, tutti ottenevano un attributo xmls vuoto (<url xmlns="">...</url>), a meno che non specifichi lo spazio dei nomi quando creo l'elemento nel modello.

Non è molto ASCIUTTO. c'è un modo per definire lo spazio dei nomi di tutti gli elementi che vengono creati?

<xsl:template match="/">
    <!-- Root element has a namespace -->
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
        <xsl:apply-templates/>
    </urlset>
</xsl:template>

<xsl:template match="Document">
    <!-- Do it this way to prevent empty xmlns attribute on element -->
    <xsl:element name="url" namespace="http://www.sitemaps.org/schemas/sitemap/0.9">
        <!-- This element will get the empty xmlns attribute, unless I create it like the url element -->
        <location>
            <xsl:value-of select="Path" />
        </location>
        <!-- There are more elements to create here, do I have to specify the namespace each time? -->
    </xsl:element>
</xsl:template>

Grazie!

È stato utile?

Soluzione

Specifica lo spazio dei nomi predefinito nella radice del foglio di stile.

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

O, a mio avviso, una soluzione preferita, definire un prefisso sulla radice e usarlo in seguito per i tuoi elementi:

<xsl:stylesheet version="1.0" xmlns:sm="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <sm:urlset>
            <xsl:apply-templates/>
        </sm:urlset>
    </xsl:template>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top