Xml XSLT:用此字符替换“\uuuuuu”:

Xml XSLT:用此字符替换“\uuuuuu”:,xml,xslt,Xml,Xslt,下面是输入XML代码,我想用冒号:字符替换任何提到这个字符序列的地方 例如,如果它是sbdh__sender,则应替换为sbdh:sender 在此方面的任何帮助都将不胜感激。XSLT <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

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

例如,如果它是sbdh__sender,则应替换为sbdh:sender

在此方面的任何帮助都将不胜感激。

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>

如在生成_u_u_u_u_u_u_uu_u_u_u_u_u_u_u_u_u_uu_uu_u_uu?这是可能的,但我建议您修复创建此xml文件的序列化程序。例如,如果它是sbdh_u_sender,则应替换为sbdh:sender,只需替换剩余输出应相同的字符。如果名称空间也添加到EPCIS文档行中。例如xmlns:gsk=namespace xmlns:sbdh=namespace您使用哪种版本的XSLT?1.0或2.0?我们使用的是xslt 1.0。我的要求是1.0,我们可以选择2.0,因为我需要在我的应用程序中尝试xslt代码是否工作,2.0感谢您的回答。我们可以将这些名称空间保留在EPCIsDocument级别,而不是gskepcextension和standardbusinessdocumentheader中。例如,节点应使用关闭。xmlns不应该进入这个节点。我相信你可以,但我不知道我是怎么想的。它不会对xml处理器产生影响。只要符合标准,请查看更新后的答案。。xslt2.0看起来像是一个更干净的选项,但它适用于1.0。您必须在两个位置设置名称空间。非常感谢您的建议。如果您有任何在GskEpcextension级别删除xmlns名称空间的想法,请告诉我。
<?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>