使用 xslt 将名称为 Standard 的元素重命名为 Product [英] Renaming an element with name Standard into Product using xslt
问题描述
我必须转换以下 xml 输入,
I had to transform following xml input,
<Standards xmlns="http://ws.wso2.org/dataservice">
<Standard>
<ProductID>200057</ProductID>
<Prefix>ISO</Prefix>
<SNumber>1001</SNumber>
<DraftProducts>
<RelatedProduct>
<ProductID>1500163</ProductID>
</RelatedProduct>
</DraftProducts>
<ReferenceProducts>
<RelatedProduct>
<ProductID>263973</ProductID>
<RelationId>708519</RelationId>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID>320056</ProductID>
<RelationId>934789</RelationId>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</ReferenceProducts>
<RelatedIntProducts>
<RelatedProduct>
<ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<RelationId>116881</RelationId>
<Relationship>Identical</Relationship>
<Designation>NEN ISO 1001</Designation>
</RelatedProduct>
<RelatedProduct>
<ProductID>208076</ProductID>
<RelationId>116886</RelationId>
<Relationship>Identical</Relationship>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</RelatedIntProducts>
<S1>1001</S1>
<S2>1986</S2>
</Standard>
<Standard>
<ProductID>200058</ProductID>
<Prefix>ISO</Prefix>
<SNumber>1002</SNumber>
<DraftProducts>
<RelatedProduct>
<ProductID>1500167</ProductID>
</RelatedProduct>
</DraftProducts>
<ReferenceProducts>
<RelatedProduct>
<ProductID>263974</ProductID>
<RelationId>708519</RelationId>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID>320052</ProductID>
<RelationId>934754</RelationId>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</ReferenceProducts>
<RelatedIntProducts>
<RelatedProduct>
<ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<RelationId>116837</RelationId>
<Relationship>Identical</Relationship>
<Designation>NEN ISO 1001</Designation>
</RelatedProduct>
<RelatedProduct>
<ProductID>208074</ProductID>
<RelationId>116843</RelationId>
<Relationship>Identical</Relationship>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</RelatedIntProducts>
<S1>1005</S1>
<S2>1983</S2>
</Standard>
</Standards>
转换成以下输出格式.
<Standards xmlns="http://ws.wso2.org/dataservice">
<Standard>
<ProductID>200057</ProductID>
<Prefix>ISO</Prefix>
<SNumber>1001</SNumber>
<RelatedProducts>
<RelatedProduct>
<ProductID>1500163</ProductID>
<RelationType>DraftProducts</RelationType>
</RelatedProduct>
<RelatedProduct>
<ProductID>263973</ProductID>
<RelationId>708519</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID>320056</ProductID>
<RelationId>934789</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<RelationId>116881</RelationId>
<Relationship>Identical</Relationship>
<RelationType>RelatedIntProducts</RelationType>
<Designation>NEN ISO 1001</Designation>
</RelatedProduct>
<RelatedProduct>
<ProductID>208076</ProductID>
<RelationId>116886</RelationId>
<RelationType>RelatedIntProducts</RelationType>
<Relationship>Identical</Relationship>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</RelatedProducts>
<S1>1001</S1>
<S2>1986</S2>
</Standard>
<Standard>
<ProductID>200058</ProductID>
<Prefix>ISO</Prefix>
<SNumber>1002</SNumber>
<RelatedProducts>
<RelatedProduct>
<ProductID>1500167</ProductID>
<RelationType>DraftProducts</RelationType>
</RelatedProduct>
<RelatedProduct>
<ProductID>263974</ProductID>
<RelationId>708519</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID>320052</ProductID>
<RelationId>934754</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<RelationId>116837</RelationId>
<RelationType>RelatedIntProducts</RelationType>
<Relationship>Identical</Relationship>
<Designation>NEN ISO 1001</Designation>
</RelatedProduct>
<RelatedProduct>
<ProductID>208074</ProductID>
<RelationId>116843</RelationId>
<RelationType>RelatedIntProducts</RelationType>
<Relationship>Identical</Relationship>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</RelatedProducts>
<S1>1005</S1>
<S2>1983</S2>
</Standard>
</Standards>
在论坛其他人的支持下,我能够编写以下 xslt 来完成此转换.
With the support from the others in the forum I was able to write the following xslt to do this transformation.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://ws.wso2.org/dataservice"
xmlns="http://ws.wso2.org/dataservice"
exclude-result-prefixes="x"
version="1.0">
<xsl:output indent="yes" method="xml" />
<!-- Identity Transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x:Standard">
<xsl:copy>
<xsl:copy-of select="x:ProductID"/>
<xsl:copy-of select="x:Prefix"/>
<xsl:copy-of select="x:SNumber"/>
<RelatedProducts>
<xsl:apply-templates select=".//x:RelatedProduct"/>
</RelatedProducts>
<xsl:copy-of select="x:S1"/>
<xsl:copy-of select="x:S2"/>
</xsl:copy>
</xsl:template>
<xsl:template match="x:RelatedProduct">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<RelationType><xsl:value-of select="name(..)"/></RelationType>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
但现在我需要将当前命名为 Standard 的所有元素重命名为 Product,最终输出应如下所示.
But now I need to rename all the elements currently named as Standard into Product and the final output should be as follows.
<Standards xmlns="http://ws.wso2.org/dataservice">
<Product>
<ProductID>200057</ProductID>
<Prefix>ISO</Prefix>
<SNumber>1001</SNumber>
<RelatedProducts>
<RelatedProduct>
<ProductID>1500163</ProductID>
<RelationType>DraftProducts</RelationType>
</RelatedProduct>
<RelatedProduct>
<ProductID>263973</ProductID>
<RelationId>708519</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID>320056</ProductID>
<RelationId>934789</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<RelationId>116881</RelationId>
<Relationship>Identical</Relationship>
<RelationType>RelatedIntProducts</RelationType>
<Designation>NEN ISO 1001</Designation>
</RelatedProduct>
<RelatedProduct>
<ProductID>208076</ProductID>
<RelationId>116886</RelationId>
<RelationType>RelatedIntProducts</RelationType>
<Relationship>Identical</Relationship>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</RelatedProducts>
<S1>1001</S1>
<S2>1986</S2>
</Product>
<Product>
<ProductID>200058</ProductID>
<Prefix>ISO</Prefix>
<SNumber>1002</SNumber>
<RelatedProducts>
<RelatedProduct>
<ProductID>1500167</ProductID>
<RelationType>DraftProducts</RelationType>
</RelatedProduct>
<RelatedProduct>
<ProductID>263974</ProductID>
<RelationId>708519</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID>320052</ProductID>
<RelationId>934754</RelationId>
<RelationType>ReferenceProducts</RelationType>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
<RelatedProduct>
<ProductID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<RelationId>116837</RelationId>
<RelationType>RelatedIntProducts</RelationType>
<Relationship>Identical</Relationship>
<Designation>NEN ISO 1001</Designation>
</RelatedProduct>
<RelatedProduct>
<ProductID>208074</ProductID>
<RelationId>116843</RelationId>
<RelationType>RelatedIntProducts</RelationType>
<Relationship>Identical</Relationship>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</RelatedProduct>
</RelatedProducts>
<S1>1005</S1>
<S2>1983</S2>
</Product>
</Standards>
我尝试了几种方法并更改了上述 xslt,但仍然没有运气.我是 xslt 的新手.我怎样才能改变上面的 xslt 来完成这件事.任何帮助将不胜感激.
I tried several approaches and changed the above xslt, but still there's no luck. I am a novice to xslt. How can I change the above xslt to get this thing done. Any help would be appreciated.
推荐答案
只需替换
<xsl:template match="x:Standard">
<xsl:copy>
<xsl:copy-of select="x:ProductID" />
with
(当然还有结束标签).不用修改其他东西就这么简单,给
with <Product>
(and the closing tag, too, of course). It's that easy without modifying something else, giving
<xsl:template match="x:Standard">
<Product>
<xsl:copy-of select="x:ProductID" />
结果是:
<?xml version="1.0"?>
<Standards xmlns="http://ws.wso2.org/dataservice">
<Product>
<ProductID>200057</ProductID>
<Prefix>ISO</Prefix>
<SNumber>1001</SNumber>
<RelatedProducts>
<RelatedProduct>
<ProductID>1500163</ProductID>
<RelationType>DraftProducts</RelationType>
</RelatedProduct>
<RelatedProduct>
<ProductID>263973</ProductID>
<Designation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<RelationType>ReferenceProducts</RelationType>
</RelatedProduct>
...
如果您还想复制 RelationId
,只需从最后一个模板中删除异常,使其看起来像这样:
If you want to also copy the RelationId
just remove the exeception from the last template, so that it looks like this:
<xsl:template match="x:RelatedProduct">
<xsl:copy>
<xsl:apply-templates select="@*|*"/>
<RelationType><xsl:value-of select="name(..)"/></RelationType>
</xsl:copy>
</xsl:template>
同时应用两者会给你准确的结果.
Applying both gives you the exact result.
这篇关于使用 xslt 将名称为 Standard 的元素重命名为 Product的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!