XSLT更改XML元素顺序
我需要使用转换重新排列源XML文件,以获得:XSLT更改XML元素顺序,xml,xslt,Xml,Xslt,我需要使用转换重新排列源XML文件,以获得: 新的元素顺序。查看源输出和所需输出之间的差异:应该出现在之后,而中的元素也必须以不同的顺序出现 将源中不存在的元素添加为输出中的空元素否则按原样复制元素(查看元素) 源文件: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="c:\ISO19139_rve.xsl"?> <MD_Metadata xmlns="http://www.isotc211.
应该出现在
之后,而
中的元素也必须以不同的顺序出现
元素)<?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">
<CI_ResponsibleParty>
<organizationName>
<gco:CharacterString>Organisation Name</gco:CharacterString>
</organizationName>
<role>
<CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Proprietario">Proprietario</CI_RoleCode>
</role>
<contactInfo>
<CI_Contact>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.mydomain.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
<phone>
<CI_Telephone>
<voice>
<gco:CharacterString>+39 123 456789</gco:CharacterString>
</voice>
</CI_Telephone>
</phone>
</CI_Contact>
</contactInfo>
</CI_ResponsibleParty>
</MD_Metadata>
<?xml version="1.0" encoding="UTF-8"?>
<?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">
<gmd:CI_ResponsibleParty>
<gmd:organisationName>
<gco:CharacterString>Organisation Name</gco:CharacterString>
</gmd:organisationName>
<gmd:contactInfo>
<gmd:CI_Contact>
<gmd:phone>
<gmd:CI_Telephone>
<gmd:voice>
<gco:CharacterString>+39 123 456789</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>
<gmd:onlineResource>
<gmd:CI_OnlineResource>
<gmd:linkage>
<gmd:URL>http://www.mydomain.it</gmd:URL>
</gmd:linkage>
</gmd:CI_OnlineResource>
</gmd:onlineResource>
</gmd:CI_Contact>
</gmd:contactInfo>
<gmd:role>
<gmd:CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="owner">Proprietario</gmd:CI_RoleCode>
</gmd:role>
</gmd:CI_ResponsibleParty>
</MD_Metadata>
<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/3.2"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
xmlns="http://www.isotc211.org/schemas/2005/gmd"
>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" encoding="UTF-8"/>
<!-- identity template -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="gmd:CI_ResponsibleParty" exclude-result-prefixes="#all">
<xsl:copy>
<!--<xsl:copy-of select="@*" />-->
<xsl:copy-of select="gmd:organizationName" />
<xsl:apply-templates select="gmd:contactInfo" />
<xsl:copy-of select="gmd:role" />
<!--<xsl:apply-templates select="node()" />-->
</xsl:copy>
</xsl:template>
<!--<xsl:template match="gmd:contactInfo" />-->
<xsl:template match="gmd:contactInfo" exclude-result-prefixes="#all">
<xsl:copy>
<xsl:copy-of select="gmd:phone" />
<xsl:apply-templates select="gmd:address" />
<xsl:if test="not(gmd:address)">
<address>
<CI_Address>
<electronicMailAddress>
<gco:CharacterString/>
</electronicMailAddress>
</CI_Address>
</address>
</xsl:if>
<xsl:copy-of select="gmd:onlineResource" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
机构名称
所有权人
http://www.mydomain.it
+39 123 456789
所需结果:
<?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">
<CI_ResponsibleParty>
<organizationName>
<gco:CharacterString>Organisation Name</gco:CharacterString>
</organizationName>
<role>
<CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Proprietario">Proprietario</CI_RoleCode>
</role>
<contactInfo>
<CI_Contact>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.mydomain.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
<phone>
<CI_Telephone>
<voice>
<gco:CharacterString>+39 123 456789</gco:CharacterString>
</voice>
</CI_Telephone>
</phone>
</CI_Contact>
</contactInfo>
</CI_ResponsibleParty>
</MD_Metadata>
<?xml version="1.0" encoding="UTF-8"?>
<?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">
<gmd:CI_ResponsibleParty>
<gmd:organisationName>
<gco:CharacterString>Organisation Name</gco:CharacterString>
</gmd:organisationName>
<gmd:contactInfo>
<gmd:CI_Contact>
<gmd:phone>
<gmd:CI_Telephone>
<gmd:voice>
<gco:CharacterString>+39 123 456789</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>
<gmd:onlineResource>
<gmd:CI_OnlineResource>
<gmd:linkage>
<gmd:URL>http://www.mydomain.it</gmd:URL>
</gmd:linkage>
</gmd:CI_OnlineResource>
</gmd:onlineResource>
</gmd:CI_Contact>
</gmd:contactInfo>
<gmd:role>
<gmd:CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="owner">Proprietario</gmd:CI_RoleCode>
</gmd:role>
</gmd:CI_ResponsibleParty>
</MD_Metadata>
<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/3.2"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
xmlns="http://www.isotc211.org/schemas/2005/gmd"
>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" encoding="UTF-8"/>
<!-- identity template -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="gmd:CI_ResponsibleParty" exclude-result-prefixes="#all">
<xsl:copy>
<!--<xsl:copy-of select="@*" />-->
<xsl:copy-of select="gmd:organizationName" />
<xsl:apply-templates select="gmd:contactInfo" />
<xsl:copy-of select="gmd:role" />
<!--<xsl:apply-templates select="node()" />-->
</xsl:copy>
</xsl:template>
<!--<xsl:template match="gmd:contactInfo" />-->
<xsl:template match="gmd:contactInfo" exclude-result-prefixes="#all">
<xsl:copy>
<xsl:copy-of select="gmd:phone" />
<xsl:apply-templates select="gmd:address" />
<xsl:if test="not(gmd:address)">
<address>
<CI_Address>
<electronicMailAddress>
<gco:CharacterString/>
</electronicMailAddress>
</CI_Address>
</address>
</xsl:if>
<xsl:copy-of select="gmd:onlineResource" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
机构名称
+39 123 456789
http://www.mydomain.it
所有权人
我的转变:
<?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">
<CI_ResponsibleParty>
<organizationName>
<gco:CharacterString>Organisation Name</gco:CharacterString>
</organizationName>
<role>
<CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="Proprietario">Proprietario</CI_RoleCode>
</role>
<contactInfo>
<CI_Contact>
<onlineResource>
<CI_OnlineResource>
<linkage>
<URL>http://www.mydomain.it</URL>
</linkage>
</CI_OnlineResource>
</onlineResource>
<phone>
<CI_Telephone>
<voice>
<gco:CharacterString>+39 123 456789</gco:CharacterString>
</voice>
</CI_Telephone>
</phone>
</CI_Contact>
</contactInfo>
</CI_ResponsibleParty>
</MD_Metadata>
<?xml version="1.0" encoding="UTF-8"?>
<?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">
<gmd:CI_ResponsibleParty>
<gmd:organisationName>
<gco:CharacterString>Organisation Name</gco:CharacterString>
</gmd:organisationName>
<gmd:contactInfo>
<gmd:CI_Contact>
<gmd:phone>
<gmd:CI_Telephone>
<gmd:voice>
<gco:CharacterString>+39 123 456789</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>
<gmd:onlineResource>
<gmd:CI_OnlineResource>
<gmd:linkage>
<gmd:URL>http://www.mydomain.it</gmd:URL>
</gmd:linkage>
</gmd:CI_OnlineResource>
</gmd:onlineResource>
</gmd:CI_Contact>
</gmd:contactInfo>
<gmd:role>
<gmd:CI_RoleCode codeList="./resource/codeList.xml#CI_RoleCode" codeListValue="owner">Proprietario</gmd:CI_RoleCode>
</gmd:role>
</gmd:CI_ResponsibleParty>
</MD_Metadata>
<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/3.2"
xmlns:gco="http://www.isotc211.org/schemas/2005/gco"
xmlns:gmd="http://www.isotc211.org/schemas/2005/gmd"
xmlns="http://www.isotc211.org/schemas/2005/gmd"
>
<xsl:strip-space elements="*"/>
<xsl:output indent="yes" encoding="UTF-8"/>
<!-- identity template -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*" />
</xsl:copy>
</xsl:template>
<xsl:template match="gmd:CI_ResponsibleParty" exclude-result-prefixes="#all">
<xsl:copy>
<!--<xsl:copy-of select="@*" />-->
<xsl:copy-of select="gmd:organizationName" />
<xsl:apply-templates select="gmd:contactInfo" />
<xsl:copy-of select="gmd:role" />
<!--<xsl:apply-templates select="node()" />-->
</xsl:copy>
</xsl:template>
<!--<xsl:template match="gmd:contactInfo" />-->
<xsl:template match="gmd:contactInfo" exclude-result-prefixes="#all">
<xsl:copy>
<xsl:copy-of select="gmd:phone" />
<xsl:apply-templates select="gmd:address" />
<xsl:if test="not(gmd:address)">
<address>
<CI_Address>
<electronicMailAddress>
<gco:CharacterString/>
</electronicMailAddress>
</CI_Address>
</address>
</xsl:if>
<xsl:copy-of select="gmd:onlineResource" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
我想你应该使用
,而不是
然而,如果你使用
,你将不得不改变这一行,可能还有十几个其他地方@foo
会被复制。这
是一个打字错误!对不起,是我的错!无论如何,我会使用
,因为我需要重新排列
子元素(正如您所做的),但也要重新排列
,它们应该出现在
之后,而不是像在源文件中那样出现在前面。所以1) 我可以只使用一个模板来执行此操作,还是需要为
案例创建一个新模板?2) 我是否需要为我要单独处理的那些元素创建像
这样的“空”模板?1)如果需要重新排序CI_ResponsibleParty
的子级,请为其编写一个模板。在其中使用一些
,就像上面的代码一样。2) 仅为要完全删除的内容创建空模板。-记住,您在这里创建的是案例处理程序<代码>读作“如果发生
,则不产生输出,继续”,这不是您想要的。好吧!因此,如果我需要从
根元素开始重新排序某些元素,我必须为它编写一个模板,其中只包含
,并为我要更改位置的每个子元素编写一些
。。。对吗?这正是它的精髓。:)我在转换名称空间时面临一些新问题。如果你有时间…:-)