首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT重命名&添加元素

XSLT重命名&添加元素
EN

Stack Overflow用户
提问于 2018-03-19 05:53:13
回答 1查看 277关注 0票数 1

目的

我试图编写一个样式表,将对应于模式版本1的实例文档转换为模式的版本2。有大约300个元素,所以我不想写一堆模板。这些版本之间的绝大多数区别是一堆标准重命名,比如删除前缀、删除以小写结尾的单词和相邻的单词之间的下划线等等。然而,我也需要适应添加和删除元素。

重命名规则

  1. 替换所有下划线字符,除非它们分隔相邻的大写字母。
  2. 删除“_of_”和“_to_Value”的所有实例。
  3. 将'_Or_‘的所有实例替换为’_或_‘。
  4. 删除“Header_”的所有实例,但“Header_Parties”和“Header_Party”前面的“Header_”除外。他们最终应该是'HeaderParties‘和'HeaderParty’。

要添加的元素

  1. <SentSequence><StatusCode>

元素删除

  1. <Line_Order>

源XML

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<Entries>
    <Entry>
        <Entry_Number>10158271304</Entry_Number>
        <CHB_File>63475017024503000</CHB_File>
        <Traffic_File>1017271467</Traffic_File>
        <Status_Code>A</Status_Code>
        <Header_Country_of_Origin>VN</Header_Country_of_Origin>
        <Importer_or_Owner>Owner</Importer_or_Owner>
        <Entry_Total_Additions_to_Value>.00</Entry_Total_Additions_to_Value>
        <Entry_IRS_Excise_Tax>.00</Entry_IRS_Excise_Tax>
        <Entry_AD_Duties>.00</Entry_AD_Duties>
        <Entry_CV_Duties>.00</Entry_CV_Duties>
        <Header_Parties>
            <Header_Party>
                <Header_Party_Type>Importer</Header_Party_Type>
            </Header_Party>
        </Header_Parties>
        <Invoices>
            <Invoice>
                <Invoice_Order>1</Invoice_Order>
                <Invoice_Lines>
                    <Invoice_Line>
                        <Line_Order>1</Line_Order>
                        <Line_Quantity>685</Line_Quantity>
                        <Entry_Lines>
                            <Entry_Line>
                                <CBP7501_Line>1</CBP7501_Line>
                            </Entry_Line>
                        </Entry_Lines>
                    </Invoice_Line>
                </Invoice_Lines>
            </Invoice>
        </Invoices>
    </Entry>
</Entries>

目标XML

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<Entries>
    <Entry>
        <EntryNumber>10158271304</EntryNumber>
        <CHB_File>63475017024503000</CHB_File>
        <TrafficFile>1017271467</TrafficFile>
        <StatusCode>A</StatusCode>
        <SentSequence>1</SentSequence>
        <CountryOrigin>VN</CountryOrigin>
        <ImporterOrOwner>Owner</ImporterOrOwner>
        <Entry_Total_Additions_to_Value>.00</Entry_Total_Additions_to_Value>
        <IRS_ExciseTax>.00</IRS_ExciseTax>
        <AD_Duties>.00</AD_Duties>
        <CV_Duties>.00</CV_Duties>
        <HeaderParties>
            <HeaderParty>
                <PartyType>Importer</PartyType>
            </HeaderParty>
        </HeaderParties>
        <Invoices>
            <Invoice>
                <InvoiceOrder>1</InvoiceOrder>
                <InvoiceLines>
                    <InvoiceLine>
                        <LineQuantity>685</LineQuantity>
                        <EntryLines>
                            <EntryLine>
                                <CBP7501Line>1</CBP7501_Line>
                            </EntryLine>
                        </EntryLines>
                    </InvoiceLine>
                </InvoiceLines>
            </Invoice>
        </Invoices>
    </Entry>
</Entries>

迄今取得的进展

我找到了几种处理重命名的不同方法,从使用函数(我最初的想法)到使用多个模板。我发现最有希望的方法是使用第二个样式表。我使用了从示例8-9派生出来的。在O‘’Reilly的XSLT烹饪书第二版中。然而,我喜欢通用外部的一个,但无法使它发挥作用。我使用的那个似乎最适合处理添加和重命名。但是,我不介意混合,因为指定所有的重命名有点效率低下。在我的例子中,我在Excel电子表格中有文档,所以我使用它来生成映射样式表的内容。

样式表

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    xmlns:kn="http://us.customsbrokerage.net/kn/cb/xsd/v1.0/functions"
    xmlns:ren="http://www.ora.com/namespaces/rename"
    xmlns:cvt="my:convert"
    exclude-result-prefixes="xs math xd"
    version="3.0">

    <!-- Mapping stylesheet -->
    <cvt:convert>
        <element rename="true" curName="Entry_Number" new="EntryNumber"/>
        <element rename="true" curName="Traffic_File" new="TrafficFile"/>
        <element rename="true" curName="Status_Code" new="StatusCode"/>
        <element add="true" curName="Status_Code" new="SentSequence"/>
        <element rename="true" curName="Header_Country_of_Origin" new="CountryOriginCode"/>
        <element rename="true" curName="Importer_or_Owner" new="ImporterOrOwner"/>
        <element rename="true" curName="Entry_Total_Additions_to_Value" new="TotalAdditions"/>
        <element rename="true" curName="Entry_IRS_Excise_Tax" new="IRS_ExciseTax"/>
        <element rename="true" curName="Entry_AD_Duties" new="AD_Duties"/>
        <element rename="true" curName="Entry_CV_Duties" new="CV_Duties"/>
        <element rename="true" curName="Header_Parties" new="HeaderParties"/>
        <element rename="true" curName="Header_Party" new="HeaderParty"/>
        <element rename="true" curName="Header_Party_Type" new="CBP_PartyType"/>
        <element rename="true" curName="Invoice_Lines" new="InvoiceLines"/>
        <element rename="true" curName="Invoice_Line" new="InvoiceLine"/>
        <element rename="true" curName="Invoice_Order" new="InvoiceOrder"/>
        <element rename="true" curName="Line_Order" new=""/>
        <element rename="true" curName="Line_Quantity" new="LineQuantity"/>
        <element rename="true" curName="Entry_Lines" new="EntryLines"/>
        <element rename="true" curName="Entry_Line" new="EntryLine"/>
        <element rename="true" curName="CBP7501_Line" new="CBP7501Line"/>
    </cvt:convert>
    <xsl:output method="xml" encoding="UTF-8" byte-order-mark="no" indent="true" version="1.0"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match=
        "*[name()=document('')/*/cvt:convert/element/@curName]">
        <xsl:variable name="convertNode" select="*[name()=document('')/*/cvt:convert/element/@curName]"/>
        <xsl:if test="$convertNode/@rename='true'">
            <xsl:element name=
                "{document('')/*/cvt:convert/element
                [@curName=name(current())]
                /@new}">
            </xsl:element>
        </xsl:if>
        <xsl:if test="*[name()=document('')/*/cvt:convert/element/@curName]/@add=true()">
            <xsl:element name=
                "{document('')/*/cvt:convert/element
                [@curName=name(current())]
                /@new}">
            </xsl:element>
        </xsl:if>
        <xsl:if test="*[name()=document('')/*/cvt:convert/element/@delete]=true()"/>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:template>
</xsl:stylesheet>

最接近我的问题似乎是这里。然而,我不太理解这种方法,无法将其移植到我的情况中。此外,它还需要XSLT3.0或使用XSLT2.0进行两步处理。我更喜欢使用XSLT2.0的一个步骤过程,但如果必须的话,我可以使用XSLT3.0。

溶液增编

作为每个人的参考,我使用了XSLT3.0方法,并进行了以下更改。见XSLT3.0解决方案更改

  • 为删除添加了一个delete="true“属性,并相应地使用@new = '‘谓词更改了模板匹配。(<xsl:key name="del-ref" match="element[@delete = 'true']" use="@curName"/><xsl:template match="*[key('del-ref', local-name(), $convert-map)]" priority="5"/>)
  • 修改mode=“新”模板,以处理同一个现有节点之后的多个添加。添加了xsl:choose来处理多个加法(<xsl:when test="count(key('add-ref', local-name(), $convert-map))>1">)或只有一个(<xsl:otherwise>)的情况。使用xsl:for-each指令迭代多个加法(<xsl:for-each select="key('add-ref', local-name(), $convert-map)"><xsl:element name="{./@new}"/>)。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-19 10:09:23

我使用了您的映射表,但仅用于重命名、元素的添加或删除,我直接实现了模板;此外,我刚刚为映射表使用了一个变量,而不是顶层元素,因为在我看来,用document('')读取样式表是更需要的技术。

生成的XSLT 3是

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="xs math map array"
    version="3.0">

  <xsl:param name="rename-map">
        <element rename="true" curName="Entry_Number" new="EntryNumber"/>
        <element rename="true" curName="Traffic_File" new="TrafficFile"/>
        <element rename="true" curName="Status_Code" new="StatusCode"/>
        <element rename="true" curName="Header_Country_of_Origin" new="CountryOriginCode"/>
        <element rename="true" curName="Importer_or_Owner" new="ImporterOrOwner"/>
        <element rename="true" curName="Entry_Total_Additions_to_Value" new="TotalAdditions"/>
        <element rename="true" curName="Entry_IRS_Excise_Tax" new="IRS_ExciseTax"/>
        <element rename="true" curName="Entry_AD_Duties" new="AD_Duties"/>
        <element rename="true" curName="Entry_CV_Duties" new="CV_Duties"/>
        <element rename="true" curName="Header_Parties" new="HeaderParties"/>
        <element rename="true" curName="Header_Party" new="HeaderParty"/>
        <element rename="true" curName="Header_Party_Type" new="CBP_PartyType"/>
        <element rename="true" curName="Invoice_Lines" new="InvoiceLines"/>
        <element rename="true" curName="Invoice_Line" new="InvoiceLine"/>
        <element rename="true" curName="Invoice_Order" new="InvoiceOrder"/>
        <element rename="true" curName="Line_Quantity" new="LineQuantity"/>
        <element rename="true" curName="Entry_Lines" new="EntryLines"/>
        <element rename="true" curName="Entry_Line" new="EntryLine"/>
        <element rename="true" curName="CBP7501_Line" new="CBP7501Line"/>
  </xsl:param>

  <xsl:key name="map-ref" match="element[@rename = 'true']" use="@curName"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="Line_Order"/>

  <xsl:template match="Status_Code">
      <xsl:next-match/>
      <SentSequence>1</SentSequence>
  </xsl:template>

  <xsl:template match="*[key('map-ref', local-name(), $rename-map)]">
      <xsl:element name="{key('map-ref', local-name(), $rename-map)/@new}">
          <xsl:apply-templates/>
      </xsl:element>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFukv8t在线上,如果您的目标是XSLT 2处理器,则用身份转换模板替换上面使用的xsl:mode指令。

代码语言:javascript
复制
<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

如果还想从映射数据中获取添加和删除的内容,则可以使用

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="3.0">

  <xsl:param name="rename-map">
       <element rename="true" curName="Entry_Number" new="EntryNumber"/>
        <element rename="true" curName="Traffic_File" new="TrafficFile"/>
        <element rename="true" curName="Status_Code" new="StatusCode"/>
        <element add="true" curName="Status_Code" new="SentSequence"/>
        <element rename="true" curName="Header_Country_of_Origin" new="CountryOriginCode"/>
        <element rename="true" curName="Importer_or_Owner" new="ImporterOrOwner"/>
        <element rename="true" curName="Entry_Total_Additions_to_Value" new="TotalAdditions"/>
        <element rename="true" curName="Entry_IRS_Excise_Tax" new="IRS_ExciseTax"/>
        <element rename="true" curName="Entry_AD_Duties" new="AD_Duties"/>
        <element rename="true" curName="Entry_CV_Duties" new="CV_Duties"/>
        <element rename="true" curName="Header_Parties" new="HeaderParties"/>
        <element rename="true" curName="Header_Party" new="HeaderParty"/>
        <element rename="true" curName="Header_Party_Type" new="CBP_PartyType"/>
        <element rename="true" curName="Invoice_Lines" new="InvoiceLines"/>
        <element rename="true" curName="Invoice_Line" new="InvoiceLine"/>
        <element rename="true" curName="Invoice_Order" new="InvoiceOrder"/>
        <element rename="true" curName="Line_Order" new=""/>
        <element rename="true" curName="Line_Quantity" new="LineQuantity"/>
        <element rename="true" curName="Entry_Lines" new="EntryLines"/>
        <element rename="true" curName="Entry_Line" new="EntryLine"/>
        <element rename="true" curName="CBP7501_Line" new="CBP7501Line"/>
  </xsl:param>

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:key name="map-ref" match="element[@rename = 'true']" use="@curName"/>
  <xsl:key name="new-ref" match="element[@add = 'true']" use="@curName"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="*[key('map-ref', local-name(), $rename-map)[@new = '']]" priority="5"/>

  <xsl:template match="Status_Code">
      <xsl:next-match/>
      <SentSequence>1</SentSequence>
  </xsl:template>

  <xsl:template match="*[key('map-ref', local-name(), $rename-map)]">
      <xsl:element name="{key('map-ref', local-name(), $rename-map)/@new}">
          <xsl:apply-templates/>
      </xsl:element>
      <xsl:apply-templates select=".[key('new-ref', local-name(), $rename-map)]" mode="new"/>
  </xsl:template>

  <xsl:template match="*" mode="new">
      <xsl:element name="{key('new-ref', local-name(), $rename-map)/@new}">1</xsl:element>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFukv8t/1

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49356594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档