루트 요소에 모든 네임 스페이스 접두사를 정의하려는 XSLT 변환을 작성하고 있습니다. 기본적으로 MS는 해당 스키마를 사용하기 위해 XML 계층 구조의 첫 번째 요소에 새 접두사 정의를 만드는 것처럼 보입니다. 즉, 동일한 스키마의 공유 조상과 관련되지 않은 요소가 여러 요소에서 참조 될 수 있습니다.
루트 요소를 이와 같이 코딩하면 모두 원하는대로 작동합니다.
<!-- ... -->
<ns0:root xmlns:ns0="http://some/schema" xmlns:ns1 = "http://another/schema">
<!-- rest of XSLT; including calls to other templates -->
</ns0:root>
<!-- ... -->
그러나 나는 이것을 사용하여 코딩하는 방법을 찾을 수 없습니다 xsl:element
. 예 :
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://some/schema"
xmlns:ns1 = "http://another/schema"
>
<!-- ... -->
<xsl:element name="ns0:root">
<xsl:attribute name="ns1" namespace="http://www.w3.org/2000/xslns/">http://another/schema</xsl:attribute>
<!-- rest of XSLT; including calls to other templates -->
</xsl:element>
<!-- ... -->
xls:element
해당 요소 자체가 아닌 for 스키마 에 대해 네임 스페이스 접두사를 선언 할 수 있습니까?
XML
<Demo xmlns="http://some/schema">
<a>Hello</a>
<b>World</b>
</Demo>
XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://some/schema"
xmlns:ns1 = "http://another/schema"
exclude-result-prefixes="xsl"
>
<xsl:output method="xml" indent="yes" version="1.0"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="/ns0:Demo/ns0:a">
<xsl:element name="ns1:z">
<xsl:value-of select="./text()" />
</xsl:element>
</xsl:template>
<xsl:template match="/ns0:Demo/ns0:b">
<xsl:element name="ns1:y">
<xsl:value-of select="./text()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
결과
<Demo xmlns="http://some/schema">
<ns1:z xmlns:ns1="http://another/schema">Hello</ns1:z>
<ns1:y xmlns:ns1="http://another/schema">World</ns1:y>
</Demo>
원하는 결과
<Demo xmlns="http://some/schema" xmlns:ns1="http://another/schema">
<ns1:z>Hello</ns1:z>
<ns1:y>World</ns1:y>
</Demo>
또는
<ns0:Demo xmlns:ns0="http://some/schema" xmlns:ns1="http://another/schema">
<ns1:z>Hello</ns1:z>
<ns1:y>World</ns1:y>
</ns0:Demo>
최소한의 예는 및 / 또는 리터럴 결과 요소 xsl:element
대신 사용해야하는 이유를 설명하지 않지만 xsl:copy
XSLT 1.0에는 xsl:namespace
지침 이 없으므로 ( https://www.w3.org/TR/xslt20/#creating-namespace-nodes ) 유일한 방법은 스타일 시트 루트에서 네임 스페이스 노드를 복사하는 것입니다.
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://some/schema"
xmlns:ns1 = "http://another/schema"
exclude-result-prefixes="xsl"
>
<xsl:output method="xml" indent="yes" version="1.0"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
<xsl:copy-of select="document('')/*/namespace::*[. = 'http://another/schema']"/>
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>
<xsl:template match="/ns0:Demo/ns0:a">
<xsl:element name="ns1:z">
<xsl:value-of select="./text()" />
</xsl:element>
</xsl:template>
<xsl:template match="/ns0:Demo/ns0:b">
<xsl:element name="ns1:y">
<xsl:value-of select="./text()" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
(또는를 갖는 다른 노드와 같은 매개 변수 또는 변수가 있지만 먼저 설정 노드에 결과 트리 조각을 변환하는 당신이 추가로 수도 그런 식으로 exsl:node-set
나 ms:node-set
).
리터럴 결과 요소와 xsl:element
다른 결과를 제공 하는 이유에 대해서는 https://www.w3.org/TR/xslt#literal-result-element에서 다음과 같이 말합니다.
생성 된 요소 노드에는 스타일 시트 트리의 요소 노드에 있던 네임 스페이스 노드의 복사본도 있습니다.
반면 https://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element 그런 말을하지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다