对我来说,一旦我通过将根元素重命名为来更正 XSLTxsl:stylesheet
代替xsl:transform
,我得到几乎正确的输出
<?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>
与您所需要的唯一语义上的显着差异version="2"
而不是version="2.0"
,可以通过添加一些引号来修复
<xsl:param name="newversion" select="'2.0'" />
将参数值设置为string2.0 而不是number 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>
I use <xsl:element>
代替<xsl:copy>
in the /*
模板因为copy
从原始标记复制命名空间绑定。
从评论中,您现在想要将所有大写<type>
价值观。您只需添加一个额外的模板即可实现这一目标
<xsl:template match="type/text()">
<xsl:value-of select="translate(., 'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')" />
</xsl:template>
没有一个简单的to-upper-case
函数与 XPath 1.0 中的一样,但您可以使用translate(X, Y, Z)
,它遍历字符串 X,将 Y 中的每个字符替换为 Z 中相同位置的字符。
这将转换all type
元素,如果您只想转换数据集类型而不是条件类型,那么只需使用更具体的match="dataset/type/text()"
.