생성 된 각 요소에 대해 XSL 스타일 시트에 기본 네임 스페이스를 지정할 수 있습니까?
문제
.NET을 사용하여 XML을 데이터 세트에서 사이트 맵 체재. 여기 내가 지금있는 곳이 있습니다. 보시다시피, 올바른 네임 스페이스로 루트 요소를 만듭니다. 자식 노드를 만들면 모두 빈 XMLS-Attribute를 받았음을 알았습니다.<url xmlns="">...</url>
), 템플릿에서 요소를 만들 때 네임 스페이스를 지정하지 않는 한.
별로 건조하지 않습니다. 생성 된 홍보 요소의 네임 스페이스를 정의하는 방법이 있습니까?
<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>
감사!
해결책
스타일 시트의 루트에 기본 네임 스페이스를 지정하십시오.
<xsl:stylesheet version="1.0" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
또는 제 생각에 선호하는 솔루션은 루트의 접두사를 정의하고 나중에 요소에 사용하십시오.
<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>
제휴하지 않습니다 StackOverflow