本文介绍了XSLT:替换“__"带有这个字符“:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是输入的 XML 代码,我想替换任何提到这个 __ 字符序列的地方,用一个冒号 : 字符替换它.

Below is input XML code and I want to replace wherever this __ character sequence is mentioned it to be replaced by a colon : character.

例如,如果是sbdh__sender,则应替换为sbdh:se​​nder.

For example, if it is sbdh__sender, it should be replace by sbdh:sender.

<?xml version="1.0" encoding="UTF-8"?>
<ns1:EPCISDocument xmlns:ns1="http://apse.com" schemaVersion="" creationDate="">
  <EPCISHeader>
    <sbdh__StandardBusinessDocumentHeader>
      <sbdh__HeaderVersion/>
      <sbdh__Sender>
        <sbdh__Identifier Authority=""/>
        <sbdh__ContactInformation>
          <sbdh__Contact/>
          <sbdh__EmailAddress/>
          <sbdh__FaxNumber/>
          <sbdh__TelephoneNumber/>
          <sbdh__ContactTypeIdentifier/>
        </sbdh__ContactInformation>
      </sbdh__Sender>
      <sbdh__Receiver>
        <sbdh__Identifier Authority=""/>
        <sbdh__ContactInformation>
          <sbdh__Contact/>
          <sbdh__EmailAddress/>
          <sbdh__FaxNumber/>
          <sbdh__TelephoneNumber/>
          <sbdh__ContactTypeIdentifier/>
        </sbdh__ContactInformation>
      </sbdh__Receiver>
      <sbdh__Manifest>
        <sbdh__NumberOfItems/>
        <sbdh__ManifestItem>
          <sbdh__MimeTypeQualifierCode/>
          <sbdh__UniformResourceIdentifier/>
          <sbdh__Description/>
          <sbdh__LanguageCode/>
        </sbdh__ManifestItem>
      </sbdh__Manifest>
      <sbdh__BusinessScope>
        <sbdh__Scope>
          <sbdh__BusinessService>
            <sbdh__BusinessServiceName/>
            <sbdh__ServiceTransaction TypeOfServiceTransaction="" IsNonRepudiationRequired="" IsAuthenticationRequired="" IsNonRepudiationOfReceiptRequired="" IsIntegrityCheckRequired="" IsApplicationErrorResponseRequired="" TimeToAcknowledgeReceipt="" TimeToAcknowledgeAcceptance="" TimeToPerform=""/>
          </sbdh__BusinessService>
          <sbdh__CorrelationInformation>
            <sbdh__RequestingDocumentCreationDateTime/>
            <sbdh__RequestingDocumentInstanceIdentifier/>
            <sbdh__ExpectedResponseDateTime/>
          </sbdh__CorrelationInformation>
        </sbdh__Scope>
      </sbdh__BusinessScope>
    </sbdh__StandardBusinessDocumentHeader>
  </EPCISHeader>
  <EPCISBody>
    <EventList>
      <ObjectEvent>
        <eventTime/>
        <recordTime/>
        <eventTimeZoneOffset/>
        <epcList>
          <epc type=""/>
        </epcList>
        <action/>
        <bizStep/>
        <disposition/>
        <readPoint>
          <id/>
        </readPoint>
        <bizLocation>
          <id/>
        </bizLocation>
        <bizTransactionList>
          <bizTransaction type=""/>
        </bizTransactionList>
        <gsk__GskEpcExtension>
          <gsk__manufacturingDate>1234</gsk__manufacturingDate>
        </gsk__GskEpcExtension>
      </ObjectEvent>
    </EventList>
  </EPCISBody>
  </ns1:EPCISDocument>

对此的任何帮助将不胜感激.

Any help on this will be appreciated.

推荐答案

XSLT ...

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ns1="http://apse.com">
  <xsl:output method="xml" indent="yes" />

  <!-- replace root element to add new namespaces -->
  <xsl:template match="ns1:EPCISDocument">
    <ns1:EPCISDocument
      xmlns:gsk="GSK Namespace Here"
      xmlns:sbdh="SBDH Namespace Here"
      >
      <xsl:apply-templates select="@* | node()"/>
    </ns1:EPCISDocument>
  </xsl:template>

  <!-- if item name has '__' then split it into QName with prefix:name -->
  <xsl:template match="@* | node()[substring-after(local-name(), '__')]">
    <xsl:variable name="prefix" select="substring-before(local-name(), '__')" />
    <xsl:variable name="name" select="substring-after(local-name(), '__')" />
    <xsl:variable name="namespace">
      <xsl:choose>
        <xsl:when test="$prefix = 'gsk'">GSK Namespace Here</xsl:when>
        <xsl:when test="$prefix = 'sbdh'">SBDH Namespace Here</xsl:when>
      </xsl:choose>
    </xsl:variable>

    <xsl:element name="{$prefix}:{$name}" namespace="{$namespace}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

  <!-- otherwise just copy the item -->
  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

...输出...

<?xml version="1.0" encoding="utf-8"?>
<ns1:EPCISDocument schemaVersion="" creationDate="" xmlns:ns1="http://apse.com" xmlns:gsk="GSK Namespace Here" xmlns:sbdh="SBDH Namespace Here">
  <EPCISHeader>
    <sbdh:StandardBusinessDocumentHeader>
      <sbdh:HeaderVersion />
      <sbdh:Sender>
        <sbdh:Identifier Authority="" />
        <sbdh:ContactInformation>
          <sbdh:Contact />
          <sbdh:EmailAddress />
          <sbdh:FaxNumber />
          <sbdh:TelephoneNumber />
          <sbdh:ContactTypeIdentifier />
        </sbdh:ContactInformation>
      </sbdh:Sender>
      <sbdh:Receiver>
        <sbdh:Identifier Authority="" />
        <sbdh:ContactInformation>
          <sbdh:Contact />
          <sbdh:EmailAddress />
          <sbdh:FaxNumber />
          <sbdh:TelephoneNumber />
          <sbdh:ContactTypeIdentifier />
        </sbdh:ContactInformation>
      </sbdh:Receiver>
      <sbdh:Manifest>
        <sbdh:NumberOfItems />
        <sbdh:ManifestItem>
          <sbdh:MimeTypeQualifierCode />
          <sbdh:UniformResourceIdentifier />
          <sbdh:Description />
          <sbdh:LanguageCode />
        </sbdh:ManifestItem>
      </sbdh:Manifest>
      <sbdh:BusinessScope>
        <sbdh:Scope>
          <sbdh:BusinessService>
            <sbdh:BusinessServiceName />
            <sbdh:ServiceTransaction TypeOfServiceTransaction="" IsNonRepudiationRequired="" IsAuthenticationRequired="" IsNonRepudiationOfReceiptRequired="" IsIntegrityCheckRequired="" IsApplicationErrorResponseRequired="" TimeToAcknowledgeReceipt="" TimeToAcknowledgeAcceptance="" TimeToPerform="" />
          </sbdh:BusinessService>
          <sbdh:CorrelationInformation>
            <sbdh:RequestingDocumentCreationDateTime />
            <sbdh:RequestingDocumentInstanceIdentifier />
            <sbdh:ExpectedResponseDateTime />
          </sbdh:CorrelationInformation>
        </sbdh:Scope>
      </sbdh:BusinessScope>
    </sbdh:StandardBusinessDocumentHeader>
  </EPCISHeader>
  <EPCISBody>
    <EventList>
      <ObjectEvent>
        <eventTime />
        <recordTime />
        <eventTimeZoneOffset />
        <epcList>
          <epc type="" />
        </epcList>
        <action />
        <bizStep />
        <disposition />
        <readPoint>
          <id />
        </readPoint>
        <bizLocation>
          <id />
        </bizLocation>
        <bizTransactionList>
          <bizTransaction type="" />
        </bizTransactionList>
        <gsk:GskEpcExtension>
          <gsk:manufacturingDate>1234</gsk:manufacturingDate>
        </gsk:GskEpcExtension>
      </ObjectEvent>
    </EventList>
  </EPCISBody>
  </ns1:EPCISDocument>

这篇关于XSLT:替换“__"带有这个字符“:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:21