从XML到XML文档的XSLT转换

从XML到XML文档的XSLT转换,xml,spring,xslt,Xml,Spring,Xslt,我需要帮助来获得正确的XSL转换,我希望源XML按原样复制,并对目标XML文件进行必要的更新。现在我正在尝试两件事,第一件是将源代码复制到目标XML,第二件是更新根元素的名称空间URL和版本属性。请查找下面的代码,并告诉我出了什么问题,因为我在目标xml中只获取根元素,缺少内容,根元素的结束标记丢失,并且属性版本未更新 源XML- <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xm

我需要帮助来获得正确的XSL转换,我希望源XML按原样复制,并对目标XML文件进行必要的更新。现在我正在尝试两件事,第一件是将源代码复制到目标XML,第二件是更新根元素的名称空间URL和版本属性。请查找下面的代码,并告诉我出了什么问题,因为我在目标xml中只获取根元素,缺少内容,根元素的结束标记丢失,并且属性版本未更新

源XML-

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:v1="http://www.abc.com/s/v1.0">
<soapenv:Header/>
<soapenv:Body>
<v1:QueryRequest version="1">
<subject>
<dataList>
<!--1 or more repetitions:-->
<dataset>
<type>company</type>
<value>abc</value>
</dataset>
<dataset>
<type>user</type>
<value>xyz</value>
</dataset>
</dataList>
</subject>
<!--Optional:-->
<testList>
<!--1 or more repetitions:-->
<criteria>
<type>test</type>
<value>972</value>
</criteria>
<criteria>
<type>test2</type>
<value>false</value>
</criteria>
</testList>
</v1:QueryRequest>
</soapenv:Body>
</soapenv:Envelope>

公司
abc
用户
xyz
测试
972
测试2
假的
XSL文件:-

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

<xsl:param name="newversion" select="2.0"> </xsl:param>
<!-- replace namespace of elements in old namespace -->

<xsl:template match="old:*">
<xsl:element name="{local-name()}" namespace="http://www.abc.com/s/v2.0">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>

<xsl:template match="//*[local-name()='QueryRequest']/@version">
<xsl:attribute name="version">
<xsl:value-of select="$newversion"/>
</xsl:attribute>

</xsl:template>     

使用上述XSL文件输出:-

<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:Header>
<soapenv:Body><ns7:QueryRequest version="1" xmlns=""
xmlns:ns6="http://www.abc.com/s/v1.0" 
xmlns:ns7="http://www.abc.com/s/v2.0"/>

</soapenv:Body>
</soapenv:Envelope>

"

预期产出:

<soapenv:Envelope 
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:v2="http://www.abc.com/s/v2.0">
<soapenv:Header/>
<soapenv:Body>
<v2:QueryRequest version="2.0">
<subject>
<dataList>
<!--1 or more repetitions:-->
<dataset>
<type>company</type>
<value>abc</value>
</dataset>
<dataset>
<type>user</type>
<value>xyz</value>
</dataset>
</dataList>
</subject>
<!--Optional:-->
<testList>
<!--1 or more repetitions:-->
<criteria>
<type>test</type>
<value>972</value>
</criteria>
<criteria>
<type>test2</type>
<value>false</value>
</criteria>
</testList>
</v2:QueryRequest>
</soapenv:Body>
</soapenv:Envelope>

公司
abc
用户
xyz
测试
972
测试2
假的
触发此转换的Spring配置代码-

<int-xml:xslt-transformer  id="v2transformer"                                     xsl-resource="classpath:transformtoversion2.xslt" 
input-channel="AChannel" output-channel="BChannel" 
result-transformer="resultTransformer"> 
</int-xml:xslt-transformer> 

对我来说,一旦我通过将根元素重命名为
xsl:stylesheet
而不是
xsl:transform
来纠正XSLT,我就几乎得到了正确的输出

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.abc.com/s/v1.0">
<soapenv:Header/>
<soapenv:Body>
<QueryRequest xmlns="http://www.abc.com/s/v2.0" version="2">
<subject xmlns="">
<dataList>
<!--1 or more repetitions:-->
<dataset>
<type>company</type>
<value>abc</value>
</dataset>
<dataset>
<type>user</type>
<value>xyz</value>
</dataset>
</dataList>
</subject>
<!--Optional:-->
<testList xmlns="">
<!--1 or more repetitions:-->
<criteria>
<type>test</type>
<value>972</value>
</criteria>
<criteria>
<type>test2</type>
<value>false</value>
</criteria>
</testList>
</QueryRequest>
</soapenv:Body>
</soapenv:Envelope>
将参数值设置为字符串2.0而不是数字2


对于外观上的差异,如果您想从根元素中删除未使用的
xmlns:v1
,并为
QueryRequest
元素使用前缀,而不是默认名称空间(对于子元素,
xmlns=“
),那么您需要类似的东西

<?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/v1.0" exclude-result-prefixes="old"
xmlns:v2="http://www.abc.com/s/v2.0">    
  <xsl:output method="xml" encoding="utf-8" indent="yes"  version="1.0" />

  <xsl:param name="newversion" select="'2.0'"/>

  <!-- fix namespace declarations on root element -->
  <xsl:template match="/*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <!-- copy the v2: binding from the stylesheet document -->
      <xsl:copy-of select="document('')/xsl:stylesheet/namespace::v2" />
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <!-- replace namespace of elements in old namespace -->
  <xsl:template match="old:*">
    <xsl:element name="v2:{local-name()}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="old:QueryRequest/@version">
    <xsl:attribute name="version">
      <xsl:value-of select="$newversion"/>
    </xsl:attribute>
  </xsl:template>     
</xsl:stylesheet>
XPath 1.0中没有简单的
到大写
函数,但是可以使用
translate(X,Y,Z)
,它通过字符串X将Y中的每个字符替换为Z中相同位置的字符


这将转换所有
type
元素,如果您只想转换数据集类型而不想转换标准类型,那么只需使用更具体的
match=“dataset/type/text()“

谢谢你的完美解决方案,就我而言,这还没有完成,因为当我使用XSL时,我得到了我所期望的元素,原因是当我将源XML转换为目标XML,然后使用版本2模式进行解组和编组,现在在版本2模式中,所有类型都是大写:-)这就是为什么输出中缺少元素…您能建议如何以简单的方式将元素值更新为大写吗?例如,我需要键入类似于公司或服务等的元素…非常好…我将尝试此…非常感谢,非常感谢。
<?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/v1.0" exclude-result-prefixes="old"
xmlns:v2="http://www.abc.com/s/v2.0">    
  <xsl:output method="xml" encoding="utf-8" indent="yes"  version="1.0" />

  <xsl:param name="newversion" select="'2.0'"/>

  <!-- fix namespace declarations on root element -->
  <xsl:template match="/*">
    <xsl:element name="{name()}" namespace="{namespace-uri()}">
      <!-- copy the v2: binding from the stylesheet document -->
      <xsl:copy-of select="document('')/xsl:stylesheet/namespace::v2" />
      <xsl:apply-templates select="@* | node()" />
    </xsl:element>
  </xsl:template>

  <!-- replace namespace of elements in old namespace -->
  <xsl:template match="old:*">
    <xsl:element name="v2:{local-name()}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="old:QueryRequest/@version">
    <xsl:attribute name="version">
      <xsl:value-of select="$newversion"/>
    </xsl:attribute>
  </xsl:template>     
</xsl:stylesheet>
<xsl:template match="type/text()">
  <xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz',
                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
</xsl:template>