2

转换文档的最快方法是什么:

<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 解析器或手动?我可以将@移动到<>的

4

4 回答 4

2

如果您决定使用 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-05-10T14:50:26.277 回答
1

直接处理 XML 数据的最佳方式是使用 XQuery。这不是最容易学习的东西,但如果您经常使用 XML,它会非常有用。

一些 IDE 甚至支持 XQuery 编辑(如 Oxygen XML)。

http://de.wikipedia.org/wiki/XQuery http://www.oxygenxml.com/

于 2009-04-03T14:10:30.907 回答
1

我认为 XSLT 是要走的路。

更多细节在这里

并使用 SAX 解析器,除非你有很好的理由。

于 2009-04-03T14:14:54.717 回答
0

下面的链接可能会有所帮助

http://www.totheriver.com/learn/xml/xmltutorial.html#5

于 2009-04-03T14:10:31.903 回答