gpt4 book ai didi

xml - XSLT 将命名空间前缀添加到元素最佳实践

转载 作者:行者123 更新时间:2023-12-04 16:54:54 26 4
gpt4 key购买 nike

出于验证目的,我需要 为每个元素添加命名空间前缀 我的转换结果文件。
我在下面写了转换,但我认为这不是做我想做的最好的方法,无论如何它不能 100% 工作......

在我的源文件中有 没有前缀的元素 我需要添加默认命名空间的前缀 gmd .但还有其他一些已指定前缀的元素 因为它们引用了其他命名空间,例如 gcogml ,并且必须维护这些。

在一些罕见的情况下,我的输入文件 可能已经设置了所有命名空间前缀 .所以我只想继续转换的其余部分(为简单起见,我在这里只包含了一个其他模板)而不添加任何内容。

我的转换有效,但是:

  • 在我剩下的转换中,我需要操作一些元素来改变子元素的顺序、名称等等......以及那些匹配另一个模板的元素,似乎与身份模板不匹配 ,所以我得到它们没有前缀。
  • 我想知道如何改进我的代码 .

  • 源文件 :
    <?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>

    XSL 转换 :
    <?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>

    如您所见,转换仅适用于与其他模板不匹配的元素。查看 <CI_Contact> 的结果, <onlineResource> , <CI_OnlineResource> , 等等。

    最佳答案

    我会写

    <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>

    作为两个模板
    <xsl:template match="node() | @*">
    <xsl:copy>
    <xsl:apply-templates select="node() | @*" />
    </xsl:copy>
    </xsl:template>

    <xsl:template match="gmd:*">
    <xsl:element name="gmd:{local-name()}" namespace="http://www.isotc211.org/schemas/2005/gmd">
    <xsl:copy-of select="namespace::*"/>
    <xsl:apply-templates select="node() | @*"/>
    </xsl:element>

    </xsl:template>

    但当然,如果您有其他模板匹配和转换 http://www.isotc211.org/schemas/2005/gmd 中的元素命名空间然后你需要确保前缀更改也在​​它们中完成,例如
    <!-- override: <CI_Contact>, reorder -->
    <xsl:template match="gmd:CI_Contact">
    <xsl:element name="gmd:{local-name()}" namespace="http://www.isotc211.org/schemas/2005/gmd">
    <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:element>
    </xsl:template>

    关于xml - XSLT 将命名空间前缀添加到元素最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23128270/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com