私はまだ XSLT に慣れていませんし、XSLT テンプレートにも問題があります。XML データの各インデント レベルに一致するテンプレートを作成しようとしていますが、何らかの理由で、apply-templates 呼び出しのバリエーションに関係なく、最初のインデント レベルしか出力できません。 . というか、1 つのインデント レベルだけを適用しているかのように、最初に出現するパターンに一致し、残りは無視します。私の XSLT 構文について何か問題があるかどうか誰かが教えてくれますか? これと同じ方法が他のデータにも使用され、うまくいきました。
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="no" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<config>
<xsl:apply-templates match="/Objects/*/*" /> <!-- Matches template #1 -->
<xsl:apply-templates match="/Objects/*/*/*" /> <!-- Matches template #2 -->
<xsl:apply-templates match="/Objects/*/*/*/*" /> <!-- Matches template #3 -->
<xsl:apply-templates match="/Objects/*/*/*/*/*" /> <!-- Matches template #4 -->
<xsl:apply-templates match="/Objects/*/*/*/*/*/*" /> <!-- Matches template #5 -->
<xsl:apply-templates match="/Objects/*/*/*/*/*/*/*" /> <!-- Matches template #6 -->
</config>
</xsl:template>
<!-- <xsl:template match="Object">
<element>
<typefield>Object</typefield>
<xsl:call-template name="elementPrinter" />
</element>
</xsl:template> -->
<!-- Begin Template #1 -->
<xsl:template match="Object/*">
<element>
<typefield>
<xsl:value-of select="@Name" />
</typefield>
<xsl:call-template name="elementPrinter" />
</element>
</xsl:template>
<!-- End Template #1 -->
<!-- Begin Template #2 -->
<xsl:template match="Object/*/*">
<element2>
<typefield>
<xsl:value-of select="@Name" />
</typefield>
<xsl:call-template name="parentIDPrinter" />
<xsl:call-template name="elementPrinter" />
</element2>
</xsl:template>
<!-- End Template #2 -->
<!-- Begin Template #3 -->
<xsl:template match="Object/*/*/*">
<element>
<typefield>
<xsl:value-of select="@Name" />
</typefield>
<xsl:call-template name="gparentIDPrinter" />
<xsl:call-template name="parentIDPrinter" />
<xsl:call-template name="elementPrinter" />
</element>
</xsl:template>
<!-- End Template #3 -->
<!-- Begin Template #4 -->
<xsl:template match="Object/*/*/*/*">
<element>
<typefield>
<xsl:value-of select="@Name" />
</typefield>
<xsl:call-template name="gparentIDPrinter" />
<xsl:call-template name="parentIDPrinter" />
<xsl:call-template name="elementPrinter" />
</element>
</xsl:template>
<!-- End Template #4 -->
<!-- Begin Template #5 -->
<xsl:template match="Object/*/*/*/*/*">
<element>
<typefield>
<xsl:value-of select="@Name" />
</typefield>
<xsl:call-template name="gparentIDPrinter" />
<xsl:call-template name="parentIDPrinter" />
<xsl:call-template name="elementPrinter" />
</element>
</xsl:template>
<!-- End Template #5 -->
<!-- Begin Template #6 -->
<xsl:template match="Object/*/*/*/*/*/*">
<element>
<typefield>
<xsl:value-of select="@Name" />
</typefield>
<xsl:call-template name="gparentIDPrinter" />
<xsl:call-template name="parentIDPrinter" />
<xsl:call-template name="elementPrinter" />
</element>
</xsl:template>
<!-- End Template #6 -->
<!-- Prints all elements within the matching node. -->
<xsl:template name="elementPrinter">
<xsl:for-each select="*">
<xsl:if test="text() != ''">
<xsl:choose>
<xsl:when test="@Name">
<xsl:variable name="eName">
<xsl:value-of select="@Name" />
</xsl:variable>
<xsl:element name="{$eName}">
<xsl:value-of select="text()" />
</xsl:element>
</xsl:when>
<xsl:when test="not(@Name)">
<xsl:variable name="eName">
<xsl:value-of select="@Type" />
</xsl:variable>
<xsl:element name="{$eName}">
<xsl:value-of select="text()" />
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- Prints a tag containing the name of the node's parent. -->
<xsl:template name="parentIDPrinter">
<xsl:for-each select="../../*[1]">
<xsl:choose>
<xsl:when test="./@Name">
<xsl:variable name="pName">
<xsl:value-of select="@Name" />
</xsl:variable>
<xsl:element name="parent">
<xsl:value-of select="$pName" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="pName">
<xsl:value-of select="@Type" />
</xsl:variable>
<xsl:element name="parent">
<xsl:value-of select="$pName" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<!-- Prints a tag containing the name of the node's grandparent. -->
<xsl:template name="gparentIDPrinter">
<xsl:for-each select="../../../../*[1]">
<xsl:choose>
<xsl:when test="./@Name">
<xsl:variable name="pName">
<xsl:value-of select="@Name" />
</xsl:variable>
<xsl:element name="grandparent">
<xsl:value-of select="$pName" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="pName">
<xsl:value-of select="@Type" />
</xsl:variable>
<xsl:element name="grandparent">
<xsl:value-of select="$pName" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
残念ながらサンプルデータは掲載できませんが、フォーマットはほぼこれだけです。
<Objects>
<Object>
<Property Name="whatever">
<Property Name="whatever1.1">whatever</Property>
<Property Name="whatever1.2">whatever2</Property>
</Property>
<Property Name="whatever">
<Property Name="whatever1.1">whatever</Property>
<Property Name="whatever1.2">whatever2</Property>
</Property>
</Object>
<Object>
<Property Name="whatever">
<Property Name="whatever1.1">whatever</Property>
<Property Name="whatever1.2">whatever2</Property>
</Property>
<Property Name="whatever">
<Property Name="whatever1.1">whatever</Property>
<Property Name="whatever1.2">whatever2</Property>
</Property>
</Object>
</Objects>
最初の apply-templates 呼び出しだけを残すと、最初のレイヤーが表示されます。これは、すべての最初のレイヤーの Property タグです...しかし、次の行を入れると、テンプレートに一致する必要があります # 2 では、テンプレート 2 のパターン データと一致する代わりに、テンプレート 1 のパターンの直後にまったく同じデータを印刷するだけです。他のテンプレートが無視されるのはなぜですか?