Xml XSLT将名称空间前缀添加到元素最佳实践
出于验证目的,我需要向转换结果文件的每个元素添加名称空间前缀。 我写了下面的转换,但我认为这不是做我想做的事情的最佳方式,而且无论如何它不能100%工作 在我的源文件中有元素没有前缀,我需要添加默认名称空间的前缀,即Xml XSLT将名称空间前缀添加到元素最佳实践,xml,xslt,namespaces,prefix,Xml,Xslt,Namespaces,Prefix,出于验证目的,我需要向转换结果文件的每个元素添加名称空间前缀。 我写了下面的转换,但我认为这不是做我想做的事情的最佳方式,而且无论如何它不能100%工作 在我的源文件中有元素没有前缀,我需要添加默认名称空间的前缀,即gmd。但是也有一些其他元素已经指定了前缀,因为它们引用了其他名称空间,例如gco和gml,这些必须维护 此外,在一些罕见的情况下,我的输入文件可能已经设置了所有名称空间前缀。所以我只想继续转换的其余部分(为了简单起见,我在这里只包含了一个其他模板),而不添加任何内容 我的转变有效,
gmd
。但是也有一些其他元素已经指定了前缀,因为它们引用了其他名称空间,例如gco
和gml
,这些必须维护
此外,在一些罕见的情况下,我的输入文件可能已经设置了所有名称空间前缀。所以我只想继续转换的其余部分(为了简单起见,我在这里只包含了一个其他模板),而不添加任何内容
我的转变有效,但:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?>
<MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/schemas/2005/gco" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd">
<fileIdentifier>
<gco:CharacterString>b0101011_Vincolo</gco:CharacterString>
</fileIdentifier>
<language>
<gco:CharacterString>IT</gco:CharacterString>
</language>
<contact>
<CI_ResponsibleParty>
<organizationName>
<gco:CharacterString>Comune di Conselve (capofila PATI)</gco:CharacterString>
</organizationName>
<role>
<CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Autore">Autore</CI_RoleCode>
</role>
<contactInfo>
<CI_Contact>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.comune.conselve.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
<phone>
<CI_Telephone>
<voice>
<gco:CharacterString>0499596511</gco:CharacterString>
</voice>
</CI_Telephone>
</phone>
</CI_Contact>
</contactInfo>
<temporalElement>
<EX_TemporalExtent>
<extent>
<gml:TimePeriod gml:id="tp1">
<gml:begin>
<gml:TimeIstant gml:id="ti1">
<gml:timePosition>2007-12-01</gml:timePosition>
</gml:TimeIstant>
</gml:begin>
<gml:end>
<gml:TimeIstant gml:id="ti2">
<gml:timePosition>2010-01-01</gml:timePosition>
</gml:TimeIstant>
</gml:end>
</gml:TimePeriod>
</extent>
</EX_TemporalExtent>
</temporalElement>
</CI_ResponsibleParty>
</contact>
</MD_Metadata>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
>
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<!-- default: identity template -->
<xsl:template match="node() | @*">
<xsl:choose>
<xsl:when test="namespace-uri() eq 'http://www.isotc211.org/schemas/2005/gmd'">
<xsl:element name="gmd:{name()}" namespace="http://www.isotc211.org/schemas/2005/gmd">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- override: <CI_Contact>, reorder -->
<xsl:template match="gmd:CI_Contact">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="gmd:phone" />
<xsl:apply-templates select="gmd:address" />
<xsl:if test="not(gmd:address)">
<gmd:address>
<gmd:CI_Address>
<gmd:electronicMailAddress>
<gco:CharacterString/>
</gmd:electronicMailAddress>
</gmd:CI_Address>
</gmd:address>
</xsl:if>
<xsl:copy-of select="gmd:onlineResource" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?>
<gmd:MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd">
<gmd:fileIdentifier>
<gco:CharacterString>b0101011_Vincolo</gco:CharacterString>
</gmd:fileIdentifier>
<gmd:language>
<gco:CharacterString>IT</gco:CharacterString>
</gmd:language>
<gmd:contact>
<gmd:CI_ResponsibleParty>
<gmd:organizationName>
<gco:CharacterString>Comune di Conselve (capofila PATI)</gco:CharacterString>
</gmd:organizationName>
<gmd:role>
<gmd:CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Autore">Autore</gmd:CI_RoleCode>
</gmd:role>
<gmd:contactInfo>
<CI_Contact>
<gmd:phone>
<gmd:CI_Telephone>
<gmd:voice>
<gco:CharacterString>0499596511</gco:CharacterString>
</gmd:voice>
</gmd:CI_Telephone>
</gmd:phone>
<gmd:address>
<gmd:CI_Address>
<gmd:electronicMailAddress>
<gco:CharacterString/>
</gmd:electronicMailAddress>
</gmd:CI_Address>
</gmd:address>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.comune.conselve.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
</CI_Contact>
</gmd:contactInfo>
<gmd:temporalElement>
<gmd:EX_TemporalExtent>
<gmd:extent>
<gml:TimePeriod gml:id="tp1">
<gml:begin>
<gml:TimeIstant gml:id="ti1">
<gml:timePosition>2007-12-01</gml:timePosition>
</gml:TimeIstant>
</gml:begin>
<gml:end>
<gml:TimeIstant gml:id="ti2">
<gml:timePosition>2010-01-01</gml:timePosition>
</gml:TimeIstant>
</gml:end>
</gml:TimePeriod>
</gmd:extent>
</gmd:EX_TemporalExtent>
</gmd:temporalElement>
</gmd:CI_ResponsibleParty>
</gmd:contact>
</gmd:MD_Metadata>
b0101011_Vincolo
信息技术
康塞尔夫司令(卡波菲拉·帕蒂)
奥托尔
http://www.comune.conselve.it
0499596511
2007-12-01
2010-01-01
XSL转换:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?>
<MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/schemas/2005/gco" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd">
<fileIdentifier>
<gco:CharacterString>b0101011_Vincolo</gco:CharacterString>
</fileIdentifier>
<language>
<gco:CharacterString>IT</gco:CharacterString>
</language>
<contact>
<CI_ResponsibleParty>
<organizationName>
<gco:CharacterString>Comune di Conselve (capofila PATI)</gco:CharacterString>
</organizationName>
<role>
<CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Autore">Autore</CI_RoleCode>
</role>
<contactInfo>
<CI_Contact>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.comune.conselve.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
<phone>
<CI_Telephone>
<voice>
<gco:CharacterString>0499596511</gco:CharacterString>
</voice>
</CI_Telephone>
</phone>
</CI_Contact>
</contactInfo>
<temporalElement>
<EX_TemporalExtent>
<extent>
<gml:TimePeriod gml:id="tp1">
<gml:begin>
<gml:TimeIstant gml:id="ti1">
<gml:timePosition>2007-12-01</gml:timePosition>
</gml:TimeIstant>
</gml:begin>
<gml:end>
<gml:TimeIstant gml:id="ti2">
<gml:timePosition>2010-01-01</gml:timePosition>
</gml:TimeIstant>
</gml:end>
</gml:TimePeriod>
</extent>
</EX_TemporalExtent>
</temporalElement>
</CI_ResponsibleParty>
</contact>
</MD_Metadata>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
>
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<!-- default: identity template -->
<xsl:template match="node() | @*">
<xsl:choose>
<xsl:when test="namespace-uri() eq 'http://www.isotc211.org/schemas/2005/gmd'">
<xsl:element name="gmd:{name()}" namespace="http://www.isotc211.org/schemas/2005/gmd">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- override: <CI_Contact>, reorder -->
<xsl:template match="gmd:CI_Contact">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="gmd:phone" />
<xsl:apply-templates select="gmd:address" />
<xsl:if test="not(gmd:address)">
<gmd:address>
<gmd:CI_Address>
<gmd:electronicMailAddress>
<gco:CharacterString/>
</gmd:electronicMailAddress>
</gmd:CI_Address>
</gmd:address>
</xsl:if>
<xsl:copy-of select="gmd:onlineResource" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?>
<gmd:MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd">
<gmd:fileIdentifier>
<gco:CharacterString>b0101011_Vincolo</gco:CharacterString>
</gmd:fileIdentifier>
<gmd:language>
<gco:CharacterString>IT</gco:CharacterString>
</gmd:language>
<gmd:contact>
<gmd:CI_ResponsibleParty>
<gmd:organizationName>
<gco:CharacterString>Comune di Conselve (capofila PATI)</gco:CharacterString>
</gmd:organizationName>
<gmd:role>
<gmd:CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Autore">Autore</gmd:CI_RoleCode>
</gmd:role>
<gmd:contactInfo>
<CI_Contact>
<gmd:phone>
<gmd:CI_Telephone>
<gmd:voice>
<gco:CharacterString>0499596511</gco:CharacterString>
</gmd:voice>
</gmd:CI_Telephone>
</gmd:phone>
<gmd:address>
<gmd:CI_Address>
<gmd:electronicMailAddress>
<gco:CharacterString/>
</gmd:electronicMailAddress>
</gmd:CI_Address>
</gmd:address>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.comune.conselve.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
</CI_Contact>
</gmd:contactInfo>
<gmd:temporalElement>
<gmd:EX_TemporalExtent>
<gmd:extent>
<gml:TimePeriod gml:id="tp1">
<gml:begin>
<gml:TimeIstant gml:id="ti1">
<gml:timePosition>2007-12-01</gml:timePosition>
</gml:TimeIstant>
</gml:begin>
<gml:end>
<gml:TimeIstant gml:id="ti2">
<gml:timePosition>2010-01-01</gml:timePosition>
</gml:TimeIstant>
</gml:end>
</gml:TimePeriod>
</gmd:extent>
</gmd:EX_TemporalExtent>
</gmd:temporalElement>
</gmd:CI_ResponsibleParty>
</gmd:contact>
</gmd:MD_Metadata>
我的实际结果:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?>
<MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:gco="http://www.isotc211.org/schemas/2005/gco" xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd">
<fileIdentifier>
<gco:CharacterString>b0101011_Vincolo</gco:CharacterString>
</fileIdentifier>
<language>
<gco:CharacterString>IT</gco:CharacterString>
</language>
<contact>
<CI_ResponsibleParty>
<organizationName>
<gco:CharacterString>Comune di Conselve (capofila PATI)</gco:CharacterString>
</organizationName>
<role>
<CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Autore">Autore</CI_RoleCode>
</role>
<contactInfo>
<CI_Contact>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.comune.conselve.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
<phone>
<CI_Telephone>
<voice>
<gco:CharacterString>0499596511</gco:CharacterString>
</voice>
</CI_Telephone>
</phone>
</CI_Contact>
</contactInfo>
<temporalElement>
<EX_TemporalExtent>
<extent>
<gml:TimePeriod gml:id="tp1">
<gml:begin>
<gml:TimeIstant gml:id="ti1">
<gml:timePosition>2007-12-01</gml:timePosition>
</gml:TimeIstant>
</gml:begin>
<gml:end>
<gml:TimeIstant gml:id="ti2">
<gml:timePosition>2010-01-01</gml:timePosition>
</gml:TimeIstant>
</gml:end>
</gml:TimePeriod>
</extent>
</EX_TemporalExtent>
</temporalElement>
</CI_ResponsibleParty>
</contact>
</MD_Metadata>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gml="http://www.opengis.net/gml"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
>
<xsl:output indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<!-- default: identity template -->
<xsl:template match="node() | @*">
<xsl:choose>
<xsl:when test="namespace-uri() eq 'http://www.isotc211.org/schemas/2005/gmd'">
<xsl:element name="gmd:{name()}" namespace="http://www.isotc211.org/schemas/2005/gmd">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- override: <CI_Contact>, reorder -->
<xsl:template match="gmd:CI_Contact">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:apply-templates select="gmd:phone" />
<xsl:apply-templates select="gmd:address" />
<xsl:if test="not(gmd:address)">
<gmd:address>
<gmd:CI_Address>
<gmd:electronicMailAddress>
<gco:CharacterString/>
</gmd:electronicMailAddress>
</gmd:CI_Address>
</gmd:address>
</xsl:if>
<xsl:copy-of select="gmd:onlineResource" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?>
<gmd:MD_Metadata xmlns="http://www.isotc211.org/schemas/2005/gmd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gml="http://www.opengis.net/gml"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
xsi:schemaLocation="http://www.isotc211.org/schemas/2005/gmd/gmd.xsd">
<gmd:fileIdentifier>
<gco:CharacterString>b0101011_Vincolo</gco:CharacterString>
</gmd:fileIdentifier>
<gmd:language>
<gco:CharacterString>IT</gco:CharacterString>
</gmd:language>
<gmd:contact>
<gmd:CI_ResponsibleParty>
<gmd:organizationName>
<gco:CharacterString>Comune di Conselve (capofila PATI)</gco:CharacterString>
</gmd:organizationName>
<gmd:role>
<gmd:CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Autore">Autore</gmd:CI_RoleCode>
</gmd:role>
<gmd:contactInfo>
<CI_Contact>
<gmd:phone>
<gmd:CI_Telephone>
<gmd:voice>
<gco:CharacterString>0499596511</gco:CharacterString>
</gmd:voice>
</gmd:CI_Telephone>
</gmd:phone>
<gmd:address>
<gmd:CI_Address>
<gmd:electronicMailAddress>
<gco:CharacterString/>
</gmd:electronicMailAddress>
</gmd:CI_Address>
</gmd:address>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.comune.conselve.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
</CI_Contact>
</gmd:contactInfo>
<gmd:temporalElement>
<gmd:EX_TemporalExtent>
<gmd:extent>
<gml:TimePeriod gml:id="tp1">
<gml:begin>
<gml:TimeIstant gml:id="ti1">
<gml:timePosition>2007-12-01</gml:timePosition>
</gml:TimeIstant>
</gml:begin>
<gml:end>
<gml:TimeIstant gml:id="ti2">
<gml:timePosition>2010-01-01</gml:timePosition>
</gml:TimeIstant>
</gml:end>
</gml:TimePeriod>
</gmd:extent>
</gmd:EX_TemporalExtent>
</gmd:temporalElement>
</gmd:CI_ResponsibleParty>
</gmd:contact>
</gmd:MD_Metadata>
b0101011_Vincolo
信息技术
康塞尔夫司令(卡波菲拉·帕蒂)
奥托尔
0499596511
http://www.comune.conselve.it
2007-12-01
2010-01-01
正如您所看到的,转换仅适用于其他模板不匹配的元素。查看
,
,
等的结果。我会写
<xsl:template match="node() | @*">
<xsl:choose>
<xsl:when test="namespace-uri() eq 'http://www.isotc211.org/schemas/2005/gmd'">
<xsl:element name="gmd:{name()}" namespace="http://www.isotc211.org/schemas/2005/gmd">
<xsl:copy-of select="namespace::*"/>
<xsl:apply-templates select="node() | @*"/>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
当您说“出于验证目的,我需要向转换结果文件的每个元素添加名称空间前缀”时,这是一种什么样的验证呢?基于正常模式的验证检查节点的名称空间,但不检查前缀,因为它们无关紧要,元素
..
与..
一样有效。是INSPIRE标准验证。还有GeoNetwork元数据导入工具。您的解决方案在这个示例XML文件上运行良好,但将其放在我的原始转换中,可以获得一些元素,其中包含所有名称空间的声明。。。因此,我找到的另一个解决方案是替换每个模板上的根元素。因此,我使用了:…