XSLT更改XML元素顺序

柴郡猫

我需要使用转换来重新排列源XML文件,以获取:

  1. 一个新的元素顺序看一下源代码和所需输出之间的区别:<gmd:role>应该在之后出现,<contactInfo>并且内部元素<CI_Contact>也必须以不同的顺序出现。
  2. 将源中不存在的元素添加为输出中的空元素,否则按原样复制该元素(查看该<address>元素)

源文件

<?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>
托玛拉克

我认为您想使用<xsl:template match="gmd:CI_contact">,而不是<xsl:template match="gmd:contactInfo">

<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)">
            <address>
                <CI_Address>
                    <electronicMailAddress>
                        <gco:CharacterString/>
                    </electronicMailAddress>
                </CI_Address>
            </address>
        </xsl:if>
        <xsl:apply-templates select="gmd:onlineResource" />
    </xsl:copy>
</xsl:template>

作为一个很好的做法:在样式表是基于身份模板,更喜欢<xsl:apply-templates><xsl:copy>

最终效果将相同(输入将被复制),但是通过这种方式,您可以轻松地添加另一个处理特殊情况的模板,而不必触摸现有模板。

假设:假设您想将所有@foo属性放到任何地方。因为上面使用<xsl:apply-templates select="@*">,所以只需附加一个问题

<xsl:template match="@foo" />

但是,如果您使用<xsl:copy-of select="@*" />它,则必须更改此行以及可能@foo要复制的其他十几个地方

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

更改重复的元素名称 xslt xml

来自分类Dev

使用 XSLT 更改 XML 元素标记

来自分类Dev

使用XSLT更改xml文件中文档元素的属性值

来自分类Dev

使用xslt更改json的顺序

来自分类Dev

XSLT:按元素顺序显示元素

来自分类Dev

XSLT:按元素顺序显示元素

来自分类Dev

如何在XSLT 1.0中首先通过复制具有特定属性的元素来更改顺序

来自分类Dev

通过XSLT 1.0更改(反向)日期顺序在元素的属性值中

来自分类Dev

如何在XSLT 1.0中首先通过复制具有特定属性的元素来更改顺序

来自分类Dev

bxSlider:更改li元素的顺序

来自分类Dev

比较XML忽略元素顺序

来自分类Dev

xml使用xslt删除元素

来自分类Dev

使用 XSLT 映射 XML 元素

来自分类Dev

更改XML元素顺序,同时保留结构层次结构和属性

来自分类Dev

将元素名称更改为其属性之一-XSLT / XML

来自分类Dev

XSLT通过元素匹配从xml复制元素

来自分类Dev

如何使用Jquery更改列表元素的顺序?

来自分类Dev

更改re.findall找到的元素的顺序

来自分类Dev

使用绝对定位更改元素的顺序

来自分类Dev

更改R中向量中元素的顺序

来自分类Dev

根据屏幕大小更改元素的顺序

来自分类Dev

更改DIV标签内的元素顺序

来自分类Dev

删除元素时PriorityQueue更改顺序

来自分类Dev

更改列表的初始元素,保持顺序

来自分类Dev

使用CSS更改HTML元素的顺序

来自分类Dev

如何使用Jquery更改列表元素的顺序?

来自分类Dev

SilverStripe:更改GridField输入元素的顺序

来自分类Dev

根据屏幕大小更改元素的顺序

来自分类Dev

如何更改JS中的子元素顺序?