从XML到XML的XSLT转换

从XML到XML的XSLT转换,xslt,Xslt,我正在尝试将所有的值更新为小写,现在一切正常。此外,我还想将名称空间从v2.0更改为v1.0,除了我不希望为下面的“category”和“authList”等子元素声明名称空间之外,它工作得很好。我想做的最重要的事情是将元素替换为以及新的数值。 因此,我想在这里实现的两件突出的事情是: 1.使用新的数值将“Name”元素更新为“Number”。(我必须这样做) 2.如果可能,请从“Category”或文档中除根元素以外的其他子元素中删除名称空间。(如有可能) 请让我知道我的XSLT有什么问题。谢

我正在尝试将所有的
值更新为小写,现在一切正常。此外,我还想将名称空间从v2.0更改为v1.0,除了我不希望为下面的“category”和“authList”等子元素声明名称空间之外,它工作得很好。我想做的最重要的事情是将
元素替换为
以及新的数值。 因此,我想在这里实现的两件突出的事情是: 1.使用新的数值将“Name”元素更新为“Number”。(我必须这样做) 2.如果可能,请从“Category”或文档中除根元素以外的其他子元素中删除名称空间。(如有可能)

请让我知道我的XSLT有什么问题。谢谢。我希望我能使问题保持简单

XML输入:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
  <ns6:QueryResponse version="2.0" xmlns="" xmlns:ns6="http://www.abc.com/s/v2.0">
     <category>
        <categoryList>
           <cat>
              <type>SUPER</type>
              <value>gg44</value>
           </cat>
           <cat>
              <type>SUPER2</type>
              <value>fff</value>
           </cat>
        </categoryList>
     </category>
     <AuthList>
        <sAuthority>
           <Name>P</Name>
    </sAuthority>               
<AuthList>               
 </ns6:QueryResponse>
 </soapenv:Body>
 </soapenv:Envelope>

超级的
gg44
超级2
fff
P
XML输出-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
  <v1:QueryResponse version="1.0" xmlns:v1="http://www.abc.com/s/v1.0">
     <category xmlns:ns2="http://www.abc.com/s/v1.0" xmlns:ns3="http://www.abc.com/s/v2.0">
        <categoryList>
           <cat>
              <type>super</type>
              <value>gg44</value>
           </cat>
           <cat>
              <type>super2</type>
              <value>fff</value>
           </cat>
        </categoryList>
     </category>
     <AuthList xmlns:ns2="http://www.abc.com/s/v1.0"  xmlns:ns3="http://www.abc.com/s/v2.0">
        <sAuthority>
           <Name>P</Name>
    </sAuthority>               
<AuthList>               
 </v1:QueryResponse>
 </soapenv:Body>
 </soapenv:Envelope>

超级的
gg44
超级2
fff
P
XSLT代码:

<?xml version="1.0" encoding="utf-8"?>
   <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="http://www.abc.com/s/v2.0"
exclude-result-prefixes="old"
xmlns:v1="http://www.abc.com/s/v1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"
   version="1.0" />

<!-- Some parameters declered -->
<xsl:param name="newversion" select="'1.0'" />   
<xsl:param name="P_1" select="'1'" />
<xsl:param name="C_2" select="'2'" />
<xsl:param name="S_3" select="'3'" />
<xsl:param name="F_4" select="'4'" />   

<!-- This is to update namespace from v2.0 to v1.0 -->
<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
</xsl:template>

<xsl:template match="old:*">
    <xsl:element name="v1:{local-name()}">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

<xsl:template match="old:QueryResponse/@version">
    <xsl:attribute name="version"> 
    <xsl:value-of select="$newversion" /> 
    </xsl:attribute>
</xsl:template>

<!-- This is to update the Name to Number -->
<xsl:template match="sAuthority/Name">
  <xsl:choose>
   <xsl:when test=".='P'">
       <xsl:element name="Number">
       <xsl:value-of select="$P_1"/>           
       </xsl:element>
   </xsl:when>
   <xsl:when test=".='C'">
       <xsl:element name="Number">
       <xsl:value-of select="$C_2"/>
       </xsl:element>                      
   </xsl:when>       
   <xsl:when test=".='S'">
       <xsl:element name="Number">
       <xsl:value-of select="$S_3"/>
       </xsl:element>          
   </xsl:when>  
   <xsl:when test=".='F'">
       <xsl:element name="Number">
       <xsl:value-of select="$F_4"/>
       </xsl:element>      
   </xsl:when>                   
  </xsl:choose>
  </xsl:template>  

 <!-- This is to update Upper case values to lower case for all the type elements in  the XML input whereever they are in the XML -->
 <xsl:template match="type/text()">
   <xsl:value-of
       select="translate (., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')" />
  </xsl:template>

  </xsl:stylesheet>

预期产出:-

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
  <v1:QueryResponse version="1.0" xmlns:v1="http://www.abc.com/s/v1.0">
     <category>
        <categoryList>
           <cat>
              <type>super</type>
              <value>gg44</value>
           </cat>
           <cat>
              <type>super2</type>
              <value>fff</value>
           </cat>
        </categoryList>
     </category>
     <AuthList>
        <sAuthority>
           <Number>1</Number>
    </sAuthority>               
<AuthList>               
 </v1:QueryResponse>
 </soapenv:Body>
 </soapenv:Envelope>

超级的
gg44
超级2
fff
1.

谢谢,

如果您希望从类别和authlist元素中删除名称空间声明,请尝试将此附加模板添加到XSLT中

<xsl:template match="*[namespace-uri()='']">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node()|@*" />
    </xsl:element>
</xsl:template>

这是因为xsl:copy将跨输入XML中的任何名称空间声明进行复制(无论是否使用),因此与其复制元素,不如创建没有声明的新元素

从将名称更改为数字,XSLT看起来已经正确地完成了这项工作。但是,我建议,不要使用<强> XSL:选择< /强>,您可以考虑编写一系列模板,如:

<xsl:template match="sAuthority/Name">
  <number>
    <xsl:apply-templates />
  </number>
</xsl:template>

<xsl:template match="Name/text()[.='P']">
  <xsl:value-of select="$P_1"/>
</xsl:template>

<xsl:template match="Name/text()[.='C']">
  <xsl:value-of select="$C_2"/>
</xsl:template>

<xsl:template match="Name/text()[.='S']">
  <xsl:value-of select="$S_3"/>
</xsl:template>

<xsl:template match="Name/text()[.='F']">
  <xsl:value-of select="$F_4"/>
</xsl:template>

如果希望从类别和authlist元素中删除名称空间声明,请尝试将此附加模板添加到XSLT中

<xsl:template match="*[namespace-uri()='']">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node()|@*" />
    </xsl:element>
</xsl:template>

这是因为xsl:copy将跨输入XML中的任何名称空间声明进行复制(无论是否使用),因此与其复制元素,不如创建没有声明的新元素

从将名称更改为数字,XSLT看起来已经正确地完成了这项工作。但是,我建议,不要使用<强> XSL:选择< /强>,您可以考虑编写一系列模板,如:

<xsl:template match="sAuthority/Name">
  <number>
    <xsl:apply-templates />
  </number>
</xsl:template>

<xsl:template match="Name/text()[.='P']">
  <xsl:value-of select="$P_1"/>
</xsl:template>

<xsl:template match="Name/text()[.='C']">
  <xsl:value-of select="$C_2"/>
</xsl:template>

<xsl:template match="Name/text()[.='S']">
  <xsl:value-of select="$S_3"/>
</xsl:template>

<xsl:template match="Name/text()[.='F']">
  <xsl:value-of select="$F_4"/>
</xsl:template>


您说要将值转换为小写,但预期的输出值为大写。您说您想从
QueryResponse
内的所有内容中删除名称空间(我只能假设),但您的输入在
QueryResponse
内的所有内容上都没有名称空间。这里似乎有很多不一致之处。抱歉,我刚刚更新了帖子。请让我知道我在这里是否有意义…并建议解决方法。还有,我的输入XML没有子元素的名称空间,它只在根元素QueryResponse上?这是您当前获得的输出吗?看起来您的输入在
QueryResponse
中的任何内容上都没有名称空间。您是不是无意中删除了它们?是的,在应用了上面共享的XSLT代码之后,我得到了XML输出。在实际输入文件中,除了根元素之外,我没有任何名称空间。原始文件太大,我刚刚减少了数据元素以发布问题。我没有看到任何名称空间与QueryResponse的任何子元素关联。我们错过什么了吗?那么,您想用名称空间做什么?如果它不在那里,那么您将如何移除它?;)另外,在XSLT中,只需检查xpath中的元素号,您说要将值转换为小写,但预期输出的值为大写。您说您想从
QueryResponse
内的所有内容中删除名称空间(我只能假设),但您的输入在
QueryResponse
内的所有内容上都没有名称空间。这里似乎有很多不一致之处。抱歉,我刚刚更新了帖子。请让我知道我在这里是否有意义…并建议解决方法。还有,我的输入XML没有子元素的名称空间,它只在根元素QueryResponse上?这是您当前获得的输出吗?看起来您的输入在
QueryResponse
中的任何内容上都没有名称空间。您是不是无意中删除了它们?是的,在应用了上面共享的XSLT代码之后,我得到了XML输出。在实际输入文件中,除了根元素之外,我没有任何名称空间。原始文件太大了,我刚刚减少了数据元素以发布问题。我没有