问题描述
我是 XSLT 新手,我需要将输入 xml 更改为输出 xml
I am new to XSLT, I need to change the input xml to the output xml
输入
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ad:AcceptDataInfo xmlns:ad="http://www.abc.com">
<ad:Product>ABC</ad:SubType>
<ad:AccountNo>123</ad:AccountNo>
<ad:Date>20140429</ad:Date>
<ad:Time>160102</ad:Time>
</ad:AcceptDataInfo>
预期输出
<Documents>
<Document>
<Prop>
<Name>Product</Name>
<Value>ABC</Value>
</Prop>
<Prop>
<Name>AccountNo</Name>
<Value>123</Value>
</Prop>
<Prop>
<Name>Date</Name>
<Value>20140429</Value>
</Prop>
<Prop>
<Name>Time</Name>
<Value>160102</Value>
</Prop>
</Document>
</Documents>
我的 xslt(不完整)
my xslt (not complete)
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
<xsl:element name="{local-name(.)}">
<xsl:apply-templates select="@* | node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name(.)}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:template>
<xsl:template match="/">
<Documents>
<Document>
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</Document>
</Documents>
</xsl:template>
</xsl:stylesheet>
网上搜了一下,只能去掉命名空间前缀,加一些标签,先谢谢了!
I've searched through the web, can only remove namespace prefix, and add some of the tags, thanks in advance!
推荐答案
仅凭一个例子很难确定转换的逻辑.我猜你想要这样的东西:
It is difficult to determine the logic of the transformation based on a single example. I am guessing you want something like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<Documents>
<Document>
<xsl:apply-templates select="*"/>
</Document>
</Documents>
</xsl:template>
<xsl:template match="*">
<Prop>
<Name><xsl:value-of select="local-name()"/></Name>
<Value><xsl:value-of select="."/></Value>
</Prop>
</xsl:template>
</xsl:stylesheet>
当以上应用于(更正!)输入时:
When the above is applied to the (corrected!) input of:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ad:AcceptDataInfo xmlns:ad="http://www.abc.com">
<ad:Product>ABC</ad:Product>
<ad:AccountNo>123</ad:AccountNo>
<ad:Date>20140429</ad:Date>
<ad:Time>160102</ad:Time>
</ad:AcceptDataInfo>
产生以下结果:
<?xml version="1.0" encoding="UTF-8"?>
<Documents>
<Document>
<Prop>
<Name>Product</Name>
<Value>ABC</Value>
</Prop>
<Prop>
<Name>AccountNo</Name>
<Value>123</Value>
</Prop>
<Prop>
<Name>Date</Name>
<Value>20140429</Value>
</Prop>
<Prop>
<Name>Time</Name>
<Value>160102</Value>
</Prop>
</Document>
</Documents>
请注意,这实际上假设除了源 XML 具有两级结构(根元素和根元素的子元素)之外,实际上没有任何关于源 XML 的信息.否则,我们可以使转换不那么通用,从而提高效率.
Note that this assumes practically nothing is known in advance about the source XML except that it has a two-level structure (root element and children of root element). Otherwise we could make the transformation less generic and as a result, more efficient.
这篇关于使用 XSLT 将 XML 格式转换为另一种 XML 格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!