当前位置: 首页 > 知识库问答 >
问题:

添加新的命名空间非根节点xslt 1.0

吉岳
2023-03-14
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <S:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <ns3:Response xmlns="ttn/xml" xmlns:ns2="rtm/xml" xmlns:ns3="ws/xml">
            <ns3:record>
                <registryID>
                    <registryNumber>232019324</registryNumber>
                </registryID>
                <ns2:Date>28-08-2019 09:12:32</ns2:Date>
                <ns2:registry>
                    <ns2:type>otp</ns2:type>
                    <sender>
                        <ID>260</ID>
                    </sender>
                </ns2:registry>
            </ns3:record>
        </ns3:Response>
    </S:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Header/>
    <S:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <ns3:Response xmlns:ns1="ttn/xml" xmlns="ttn/xml" xmlns:ns2="rtm/xml" xmlns:ns3="ws/xml">
            <record>
                <ns1:registryID>
                    <ns1:registryNumber>232019324</ns1:registryNumber>
                </ns1:registryID>
                <ns2:Date>28-08-2019 09:12:32</ns2:Date>
                <ns2:registry>
                    <ns2:type>otp</ns2:type>
                    <ns1:sender>
                        <ns1:ID>260</ns1:ID>
                    </ns1:sender>
                </ns2:registry>
            </record>
        </ns3:Response>
    </S:Body>
</soapenv:Envelope>

我需要添加/删除一些前缀节点。例如节点 -> 或节点 -> 或节点 -> ....有更多的节点xml输入,但我只放了几个。

我需要在节点响应中包含ns1,因为当我导入到SAP PO时,它会说“ns1未声明”。我想我给节点添加了ns1前缀,但我不知道。我尝试在测试SAP中手动将ns1包含在节点中并进行操作。在其他转换xslt之后,只有我需要所有xml的节点响应来映射。

这是im使用的xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns="ttn/xml" xmlns:ns2="rtm/xml" xmlns:ns3="ws/xml"     xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
            xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

<xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:variable name ="mynoderecord">record</xsl:variable>
<xsl:variable name ="mynodeResponse">Response</xsl:variable> 
<xsl:variable name ="mynoderegistryNumber">registryNumber</xsl:variable>
<xsl:variable name ="mynodeRegistryID">registryID</xsl:variable>
<xsl:variable name ="mynodesender">sender</xsl:variable>


  <xsl:template match="*" >
  <xsl:choose>

    <xsl:when test="local-name() = $mynodeResponse">
     <xsl:element name="ns3:{local-name()}" xmlns:ns3="ws/xml">
    <xsl:attribute name="ns1:nsdf" namespace="ttn/xml">sdf</xsl:attribute>

       <xsl:for-each select="@*">
           <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
    </xsl:when>


    <xsl:when test="local-name() = $mynoderegistryNumber" >
     <xsl:element name="ns1:{name()}" xmlns:ns1="ttn/xml">
       <!-- procesar atributos de nodo -->
       <xsl:for-each select="@*">
       <!-- eliminar prefijo de atributo -->
         <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
    </xsl:when>

    <xsl:when test="local-name() = $mynodesender" >
     <xsl:element name="ns1:{name()}" xmlns:ns1="ttn/xml">
       <!-- procesar atributos de nodo -->
       <xsl:for-each select="@*">
       <!-- eliminar prefijo de atributo -->
         <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
    </xsl:when>

    <xsl:when test="local-name() = $mynodeRegistryID" >
     <xsl:element name="ns1:{name()}" xmlns:ns1="ttn/xml">
       <!-- procesar atributos de nodo -->
       <xsl:for-each select="@*">
       <!-- eliminar prefijo de atributo -->
         <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
    </xsl:when>

    <xsl:when test="local-name() = $mynoderecord">
     <xsl:element name="{local-name()}" xmlns:ns3="ws/xml">
       <!-- procesar atributos de nodo -->
       <xsl:for-each select="@*">
       <!-- eliminar prefijo de atributo -->
         <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
    </xsl:when>


    <xsl:otherwise>
     <xsl:element name="{name()}">
       <!-- procesar atributos de nodo -->
       <xsl:for-each select="@*">
       <!-- eliminar prefijo de atributo -->
         <xsl:attribute name="{local-name()}">
           <xsl:value-of select="."/>
         </xsl:attribute>
       </xsl:for-each>
      <xsl:apply-templates/>
     </xsl:element>
     </xsl:otherwise>

  </xsl:choose>

  </xsl:template>

</xsl:stylesheet>

共有1个答案

蔚学林
2023-03-14

这可能返回也可能不返回您期望得到的确切结果,这取决于XSLT处理器的心血来潮:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="ttn/xml"
xmlns:ns3="ws/xml">
<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>

<xsl:template match="ns3:Response">
     <ns3:Response xmlns:ns1="ttn/xml" xmlns="ttn/xml" xmlns:ns2="rtm/xml" xmlns:ns3="ws/xml">
        <xsl:apply-templates/>
    </ns3:Response>
</xsl:template>

<xsl:template match="ns3:record">
    <record xmlns="ttn/xml">
        <xsl:apply-templates/>
    </record>
</xsl:template>

<xsl:template match="ns1:*">
    <xsl:element name="ns1:{local-name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

注意,只有第三个模板做了实质性的事情:它将ns3:record移动到一个不同的名称空间。其他模板是纯化妆品,不应该是必需的。

 类似资料:
  • 我正在生成一个XML Xades签名。我需要在标记签名中添加名称空间http://uri.etsi.org/01903/v1.3.2#。 如果我在对文档签名后添加此标记,我将得到无效签名错误。 我需要命名空间将在标记签名中而不是标记对象中

  • 我正在尝试使用源代码xml的脚本,其结果可在下面的小提琴工具链接中获得 https://xsltfiddle.liberty-development.net/jxN9PRK/4 源XML: 使用的XSL脚本: 预期产出: 相反,我收到了下面的结果 XSL脚本面临两个问题, > XSL在转义时从根目录到子目录包含一个名称空间。xmlns:xsi=”http://www.w3.org/2001/XML

  • gcc和msvc无法编译这段代码,错误消息< code >命名空间范围的匿名聚合必须是静态的。但是clang编译这个没有问题。https://godbolt.org/z/WecT6vP91 https://en.cppreference.com/w/cpp/language/union说 命名空间范围匿名联合必须声明为静态,除非它们出现在未命名的命名空间中。 这似乎是gcc和msvc的错误? 已编

  • 我正在将XML返回到一个jsp页面。 最终的XML结构如下所示: 的根节点作为向量返回,我使用xtream将其别名为“tasks”,如上图所示。 如何使用XStream实现这一点?我需要使用XSLT文件来转换XML输出吗?完成这一点的最佳方法是什么?

  • 我有几个关于JAXB编组的简单问题。我正在尝试封送包含以下字段的类: 只需使用以下序列化代码: 我得到的输出是: 现在,我面临的问题如下: > 我想要名称空间xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“出现在根元素中,即TokenRestrictionTemplate中,而不是单个子元素中。如何实现这一点? 我有一些元素,例如带有@Xm

  • 我想更改模式命名空间从 到 因为它支持“覆盖”。但它显示错误: W3C XML模式的根元素应该是