转换文档的最快方法是什么,比如:
<customermodel:Customer>
<creditCards>
<cardNumber>@0</cardNumber>
<provider>@HSBC</provider>
<xsi:type>@customermodel:CreditCard</xsi:type>
23242552
</creditCards>
.
.这样,带有@的元素将成为父元素的属性。
也就是说:
<customermodel:Customer>
<creditCards cardNumber="0" provider="HSBC" xsi-type="customermodel:CreditCard>
232323232
</creditCards>
.
.使用dom?或Sax解析器或手动?我可以把@移到<>的
发布于 2009-05-10 14:50:26
如果您决定使用XSLT,它将如下所示
<!-- process element and attributes first so that whitespace doesn't interfere -->
<xsl:template match="creditCards">
<xsl:copy>
<xsl:apply-templates select="* | @*"/>
<xsl:apply-templates select="text()"/>
</xsl:copy>
</xsl:template>
<!-- change childrent of creditCards to attributes and strip first charcter from value -->
<xsl:template match="creditCards/*">
<xsl:attribute name="{name()}">
<xsl:value-of select="substring(., 2)"/>
</xsl:attribute>
</xsl:template>
<!-- rename xsi:type -->
<xsl:template match="creditCards/xsi:type">
<xsl:attribute name="xsi-type">
<xsl:value-of select="substring(., 2)"/>
</xsl:attribute>
</xsl:template>
<!-- identity transform -->
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>发布于 2009-04-03 14:10:31
直接处理XML数据的最好方法是使用XQuery。这并不是最容易学习的东西,但是如果您经常使用XML,那么它将非常有用。
一些集成开发环境甚至支持XQuery编辑(比如Oxygen XML)。
http://de.wikipedia.org/wiki/XQuery http://www.oxygenxml.com/
发布于 2009-04-03 14:14:55
我认为XSLT是个不错的选择。
更多详细信息here
并使用SAX解析器,除非您有很好的理由。
https://stackoverflow.com/questions/714056
复制相似问题