1

私の入力xmlは、json名前空間で次のようになります..

<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <json:object name="Login">
        <json:object name="Group">
            <json:object name="TargetSystem">
                <json:string name="Name">john</json:string>
                <json:string name="Password"/>
            </json:object>
        </json:object>
    </json:object>
</json:object>

このような出力が必要です

<Login>
    <Group>
        <TargetSystem>
            <Name>john</Name>
            <Password/>
        </TargetSystem>
    </Group>
</Login>

xslt を使用して入力 xml の属性名の値を使用して、この xml を作成する必要があります。以下のxsltを使用しています。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
    <xsl:template match="/">

        <xsl:apply-templates select="json:object/*"/>
    </xsl:template>
    <xsl:template match="json:object">
        <xsl:element name="{@name}">
            <xsl:value-of select="."/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

これを使用すると、ジョンだけが得られます。ループの概念を使用する必要があります..どうすればこれを達成できるか教えてください。

4

2 に答える 2

1

最小限の XSLT が必要な場合は、任意の名前空間で動作する汎用 XSLT を考え出すことができます。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>

   <xsl:template match="*[@name]">
      <xsl:element name="{@name}">
       <xsl:apply-templates/>
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

つまり、これは@name属性を持つすべての要素に一致し、代わりにその名前の要素を作成します。ルート要素にはそのような属性がないため、出力されませんが、デフォルトのテンプレート マッチはその子の処理を続行します。

于 2012-10-09T11:57:11.937 に答える
0

私はあなたが欲しいと思います

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx"
  exclude-result-prefixes="json">

<xsl:template match="/json:object">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="json:object[@name]">
  <xsl:element name="{@name}">
    <xsl:apply-templates/>
  </xsl:element>
</xsl:template>

<xsl:template match="json:string[@name]">
  <xsl:element name="{@name}">
    <xsl:value-of select="."/>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>
于 2012-10-09T11:36:32.573 に答える