0

タイトル属性をどのように表示しますか?

@nameからデータを抽出したいと思います。

<specs>
<spec name="b_homologation">1</spec>
<spec name="s_homologation_type">mo</spec>
<spec name="b_xl">0</spec>
<spec name="b_runflat">0</spec>
<spec name="s_consumption">e</spec>
<spec name="i_noise">72</spec>
<spec name="s_grip">c</spec>
</specs>

結果は次のようになります。

<field name="b_homologation">1</field>
<field name="s_homologation_type">mo</field>
...

ありがとう。

編集:

  <xsl:template match="*|@*">
    <xsl:call-template name="field">
      <xsl:with-param name="value" select="."/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="field">
    <xsl:param name="name" select="name()" />
    <xsl:param name="value" select="text()" />
      <field name="{$name}">
        <xsl:value-of select="$value" />
      </field>
  </xsl:template>

結果は(正しくありません):

<field name="specs">1mo00e72c</field>
4

2 に答える 2

2

コメントで提案したように、ここで名前付きテンプレートとパラメーターをいじる必要はありません。微調整を加えた単純な恒等変換です。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- copy input to output verbatim ... -->
  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <!-- ... except spec elements, whose name changes to field -->
  <xsl:template match="spec">
    <field><xsl:apply-templates select="@*|node()" /></field>
  </xsl:template>
</xsl:stylesheet>

これにより、

<specs>
<field name="b_homologation">1</field>
<field name="s_homologation_type">mo</field>
<field name="b_xl">0</field>
<field name="b_runflat">0</field>
<field name="s_consumption">e</field>
<field name="i_noise">72</field>
<field name="s_grip">c</field>
</specs>

ルート要素の名前を のような名前に変更する場合は、同じトリックを使用できますが、出力を整形式の XML にしたい場合は、ルートspecs要素を完全に除外することはできません。fields

于 2013-06-26T14:46:01.443 に答える
-1

このようなもの?それをテストしませんでした、いくつかの小さなバグを含めることができます:)

<xsl:template match="spec" >
    <xsl:call-template name="outputToXml" >
        <xsl:with-param name="name" select="@name" />
        <xsl:with-param name="value" select="." />
    </xsl:call-template>
</xsl:template>

<xsl:template name="outputToXml" >
    <xsl:param name="name" />
    <xsl:param name="value" />

    <xsl:text>&lt;field name="</xsl:text>
        <xsl:value-of select="$name" />
        <xsl:text>"&gt;</xsl:text>
        <xsl:value-of select="$value" />
        <xsl:text>&lt;/field&gt;</xsl:text>
</xsl:template>
于 2013-06-26T14:41:43.990 に答える