XSLT:替换" __"这个角色":"

时间:2017-01-05 18:43:54

标签: xml xslt

下面是输入XML代码,我希望在提到这个__字符序列的地方替换它,用冒号:字符替换它。

例如,如果是sbdh__sender,则应由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>

对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

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>