将xlst转换合并为一个

时间:2017-05-24 19:01:25

标签: xml xslt

我有一些用于修改xml文档的xslt文件。 ArcGIS Desktop 10.4的元数据xml。目前我将它们链接在一起以获得所需的输出xml(将xslt 1应用于输入xml,然后将xslt 2应用于第一步的输出,然后应用xslt 3)。虽然这很好但我想将我的三张纸合二为一。我尝试了,但是当我将它们组合起来时,每个部分都会“覆盖”最后一个部分。

这将删除所有不需要的节点。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no" />
   <!-- process the metadata using the templates below -->
   <xsl:template match="/">
      <xsl:apply-templates select="node() | @*" />
   </xsl:template>
   <!-- copy all metadata conent -->
   <xsl:template match="node() | @*" priority="0">
      <xsl:copy>
         <xsl:apply-templates select="node() | @*" />
      </xsl:copy>
   </xsl:template>
   <!--  If the element exists, remove it -->
   <xsl:template match="resConst | Process | mdContact | citRespParty | idPoC | idCredit | prcStep | rpIndName | rpOrgName | rpPosName | role | displayName | rpCntInfo | searchKeys | themeKeys" />
</xsl:stylesheet>

这增加了一些使用限制。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no" />
   <!-- process the metadata using the templates below -->
   <xsl:template match="/">
      <xsl:apply-templates select="node() | @*" />
   </xsl:template>
   <!-- copy all metadata conent -->
   <xsl:template match="node() | @*" priority="0">
      <xsl:copy>
         <xsl:apply-templates select="node() | @*" />
      </xsl:copy>
   </xsl:template>
   <!-- Add Nodes -->
   <xsl:template match="dataIdInfo">
      <xsl:copy>
         <xsl:copy-of select="node() | @*" />
         <resConst>
            <Consts>
               <useLimit>CONFIDENTIAL AND PROPRIETARY INFORMATION</useLimit>
            </Consts>
         </resConst>
         <searchKeys>
            <keyword>keyword</keyword>
         </searchKeys>
         <themeKeys>
            <keyword>keyword</keyword>
         </themeKeys>
    <idCredit>our org</idCredit>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

最后,这个增加了一个联系点。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="no" />
   <!-- process the metadata using the templates below -->
   <xsl:template match="/">
      <xsl:apply-templates select="node() | @*" />
   </xsl:template>
   <!-- copy all metadata conent -->
   <xsl:template match="node() | @*" priority="0">
      <xsl:copy>
         <xsl:apply-templates select="node() | @*" />
      </xsl:copy>
   </xsl:template>
   <!-- Add Nodes -->
   <xsl:template match="idCitation">
      <xsl:copy>
      <xsl:copy-of select="node() | @*" />
         <citRespParty>
            <rpIndName>name</rpIndName>
            <rpOrgName>org</rpOrgName>
            <rpPosName>role</rpPosName>
            <role>
               <RoleCd value="007" />
            </role>
            <rpCntInfo>
               <cntAddress addressType="">
                  <delPoint>street number</delPoint>
                  <city>city</city>
                  <adminArea>state</adminArea>
                  <postCode>zip</postCode>
                  <eMailAdd>email</eMailAdd>
                  <country>country</country>
               </cntAddress>
               <cntPhone>
                  <voiceNum tddtty="">phone number</voiceNum>
               </cntPhone>
            </rpCntInfo>
         </citRespParty>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

示例输入: http://textuploader.com/d92rd

示例输出: http://textuploader.com/d92r6

示例节点更改:您将看到resConsts是节点已完全删除。然后使用我们的组织使用限制文本在同一个XPath中添加一个新的resConsts节点。

这是基于我的流程的Esri网站: http://desktop.arcgis.com/en/arcmap/10.3/manage-data/metadata/editing-metadata-for-many-arcgis-items.htm

2 个答案:

答案 0 :(得分:0)

考虑使用XProc

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc"
    xmlns:c="http://www.w3.org/ns/xproc-step" version="1.0">
    <p:input port="source">
        <p:document href="input-file.xml"></p:document>
    </p:input>
    <p:output port="result"/>
    <p:xslt>
        <p:input port="stylesheet">
            <p:document href="sheet1.xsl"/>
        </p:input>
        <p:input port="parameters">
            <p:empty/>
        </p:input>
    </p:xslt>
    <p:xslt>
        <p:input port="stylesheet">
            <p:document href="sheet2.xsl"/>
        </p:input>
        <p:input port="parameters">
            <p:empty/>
        </p:input>
    </p:xslt>
    <p:xslt>
        <p:input port="stylesheet">
            <p:document href="sheet3.xsl"/>
        </p:input>
        <p:input port="parameters">
            <p:empty/>
        </p:input>
    </p:xslt>

</p:declare-step>

或某些特定于处理器的链接(http://saxonica.com/html/documentation/extensions/output-extras/serialization-parameters.htmlsaxon:next-in-chain)。

如果您想手动将三个样式表合并到一个样式表中,请了解如何使用modes并设置它们:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="@* | node()" mode="#all">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()" mode="#current"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="resConst | Process | mdContact | citRespParty | idPoC | idCredit | prcStep | rpIndName | rpOrgName | rpPosName | role | displayName | rpCntInfo | searchKeys | themeKeys" 
 mode="step1"/>

   <xsl:template match="dataIdInfo" mode="step2">
      <xsl:copy>
         <xsl:copy-of select="node() | @*" />
         <resConst>
            <Consts>
               <useLimit>CONFIDENTIAL AND PROPRIETARY INFORMATION</useLimit>
            </Consts>
         </resConst>
         <searchKeys>
            <keyword>keyword</keyword>
         </searchKeys>
         <themeKeys>
            <keyword>keyword</keyword>
         </themeKeys>
    <idCredit>our org</idCredit>
      </xsl:copy>
   </xsl:template>

<xsl:variable name="step1">
  <xsl:apply-templates mode="step1"/>
</xsl:variable>

<xsl:variable name="step2">
  <xsl:apply-templates select="$step1/node()" mode="step2"/>
</xsl:variable>

<xsl:template match="/">
  <xsl:apply-templates select="$step2/node()"/>
</xsl:template>

   <xsl:template match="idCitation">
      <xsl:copy>
      <xsl:copy-of select="node() | @*" />
         <citRespParty>
            <rpIndName>name</rpIndName>
            <rpOrgName>org</rpOrgName>
            <rpPosName>role</rpPosName>
            <role>
               <RoleCd value="007" />
            </role>
            <rpCntInfo>
               <cntAddress addressType="">
                  <delPoint>street number</delPoint>
                  <city>city</city>
                  <adminArea>state</adminArea>
                  <postCode>zip</postCode>
                  <eMailAdd>email</eMailAdd>
                  <country>country</country>
               </cntAddress>
               <cntPhone>
                  <voiceNum tddtty="">phone number</voiceNum>
               </cntPhone>
            </rpCntInfo>
         </citRespParty>
      </xsl:copy>
   </xsl:template>

</xsl:stylesheet>

键入浏览器并未经测试,但应显示原则。

答案 1 :(得分:0)

  

我尝试了,但是当我将它们组合起来时,每个部分都会“覆盖”最后一个部分。

如果没有看到输入的示例,我们只能猜测为什么会发生这种情况。我的猜测是你正在使用一些模板来复制节点,而你应该应用其他模板。复制节点会将其直接写入结果树 - 其他模板无法访问。

如果我的猜测是正确的,那么您只需要更改所有出现的内容:

<xsl:copy-of select="node() | @*" />

为:

<xsl:apply-templates select="node() | @*" />

<强> XSLT

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!--  remove selected elements (and their descendants) -->
<xsl:template match="resConst | Process | mdContact | citRespParty | idPoC | idCredit | prcStep | rpIndName | rpOrgName | rpPosName | role | displayName | rpCntInfo | searchKeys | themeKeys" />

<!-- add nodes to dataIdInfo -->
<xsl:template match="dataIdInfo">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*" />
        <resConst>
            <Consts>
                <useLimit>CONFIDENTIAL AND PROPRIETARY INFORMATION</useLimit>
            </Consts>
        </resConst>
        <searchKeys>
            <keyword>keyword</keyword>
        </searchKeys>
        <themeKeys>
            <keyword>keyword</keyword>
        </themeKeys>
        <idCredit>our org</idCredit>
    </xsl:copy>
</xsl:template>

<!-- add nodes to idCitation -->
<xsl:template match="idCitation">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*" />
        <citRespParty>
            <rpIndName>name</rpIndName>
            <rpOrgName>org</rpOrgName>
            <rpPosName>role</rpPosName>
            <role>
                <RoleCd value="007" />
            </role>
            <rpCntInfo>
                <cntAddress addressType="">
                    <delPoint>street number</delPoint>
                    <city>city</city>
                    <adminArea>state</adminArea>
                    <postCode>zip</postCode>
                    <eMailAdd>email</eMailAdd>
                    <country>country</country>
                </cntAddress>
                <cntPhone>
                    <voiceNum tddtty="">phone number</voiceNum>
                </cntPhone>
            </rpCntInfo>
        </citRespParty>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
相关问题