【发布时间】:2017-02-22 21:36:04
【问题描述】:
例如这是我的 XML 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<products>
<product>
<code>37</code>
<ws_code>T37</ws_code>
<barcode>11111111</barcode>
</product>
</products>
我想改成这样:
<?xml version="1.0" encoding="UTF-8" ?>
<products>
<product>
<ProductCode>37</ProductCode>
<ProductBarcode>11111111</ProductBarcode>
</product>
</products>
我正在处理一个 XSLT 文件(缺少):
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="product/code">
<ProductCode><xsl:apply-templates select="@*|node()" /></ProductCode>
</xsl:template>
<xsl:template match="product/barcode">
<ProductBarcode><xsl:apply-templates select="@*|node()" /></ProductBarcode>
</xsl:template>
</xsl:stylesheet>
实际上,XML 文件来自服务器,我无法对此文件进行任何更改。
如何使用 XSLT 更改这些元素名称并删除一些标签,以及如何链接 XSLT 和 XML 文件?
要更改的标签名称是:
代码 --> 产品代码
ws_code --> 应该删除
条码 --> ProductBarcode
【问题讨论】:
-
很高兴您提供了输入 XML 和您想要的输出的清晰示例。您能否也发布您迄今为止尝试过的 XSLT?
-
code-->ProductCode转换为<xsl:template match="code"><ProductCode><xsl:apply-templates/></ProductCode></xsl:template>,将ws_code删除为<xsl:template match="ws_code"/>,然后添加身份转换模板并按照该方法使用其他模板实施其他必要的更改。 -
我在问题中添加了我的 xslt 文件示例。但我不知道如何将我的 xslt 文件连接到 xml。
-
@D.Dirik "我不知道如何将我的 xslt 文件连接到 xml。" 这取决于您将使用什么来执行转换。
-
@BenL 我添加了我的 xslt 文件。