1

次のような構造を持つxmlスキーマがあります

<Level>
<Level1>...data...</Level1>
<Level2>...data...</Level2>
.
.
.
</Level>

<Level>タグを消したいです。xslt の助けを借りて、どうすればよいのでしょうか。

4

3 に答える 3

2

「XMLの大部分を同じに保ちながら、その一部を微調整する方法」の質問に対する標準的な答えは、「IDテンプレートを使用して、変更したい特定のものに対してオーバーライドする」です。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- omit the <?xml?> line in the output, it won't be well-formed anyway -->
  <xsl:output method="xml" omit-xml-declaration="yes" />

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@*|node()" /></xsl:copy>
  </xsl:template>

  <xsl:template match="/*">
    <xsl:apply-templates select="node()" />
  </xsl:template>
</xsl:stylesheet>

しかし、リスター氏がコメントで指摘しているように、これにより、複数のドキュメント要素が含まれるため、整形式のXMLではないものが残ります。

このスタイルシートを入力XMLに適用すると

<Level>
<Level1>...data...</Level1>
<Level2>...data...</Level2>
</Level>

結果を出します

<Level1>...data...</Level1>
<Level2>...data...</Level2>
于 2013-01-15T12:21:52.840 に答える
0

コードの助けを借りて要件を満たす新しい XSLT 定義を作成しました

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="utf-8" method="text" omit-xml-declaration="yes"/>
<xsl:variable name="nl">
    <xsl:text/>
</xsl:variable>
<xsl:variable name="tb">
    <xsl:text/>
</xsl:variable>
<xsl:template match="/*">
    <!-- Open the root array -->
    <xsl:text>[{</xsl:text>
    <xsl:value-of select="$nl"/>
    <!-- Process all the child nodes of the root -->
    <xsl:apply-templates mode="detect" select="*">
        <xsl:with-param name="indent" select="$tb"/>
    </xsl:apply-templates>
    <!-- Close the root array -->
    <xsl:value-of select="$nl"/>
    <xsl:text>}]</xsl:text>
</xsl:template>
<xsl:template match="*" mode="detect">
    <xsl:choose>
        <xsl:when test="name(preceding-sibling::*[1]) = name(current()) and name(following-sibling::*[1]) != name(current())">
            <xsl:apply-templates mode="obj-content" select="."/>
            <xsl:text>]</xsl:text>
            <xsl:if test="count(following-sibling::*[name() != name(current())]) > 0">, </xsl:if>
        </xsl:when>
        <xsl:when test="name(preceding-sibling::*[1]) = name(current())">
            <xsl:apply-templates mode="obj-content" select="."/>
            <xsl:if test="name(following-sibling::*) = name(current())">, </xsl:if>
        </xsl:when>
        <xsl:when test="following-sibling::*[1][name() = name(current())]">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="name()"/>
            <xsl:text>" : [</xsl:text>
            <xsl:apply-templates mode="obj-content" select="."/>
            <xsl:text>, </xsl:text>
        </xsl:when>
        <xsl:when test="count(./child::*) > 0 or count(@*) > 0">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="name()"/>" : [<xsl:apply-templates
                mode="obj-content" select="."/>
            <xsl:if test="count(following-sibling::*) > 0">], </xsl:if>
        </xsl:when>
        <xsl:when test="count(./child::*) = 0">
            <xsl:text>"</xsl:text>
            <xsl:value-of select="name()"/>" : "<xsl:apply-templates select="."/>
            <xsl:text>"</xsl:text>
            <xsl:if test="count(following-sibling::*) > 0">, </xsl:if>
        </xsl:when>
    </xsl:choose>
</xsl:template>
<xsl:template match="*" mode="obj-content">
    <xsl:text>{</xsl:text>
    <xsl:apply-templates mode="attr" select="@*"/>
    <xsl:if test="count(@*) > 0 and (count(child::*) > 0 or text())">, </xsl:if>
    <xsl:apply-templates mode="detect" select="./*"/>
    <xsl:if test="count(child::*) = 0 and text() and not(@*)">
        <xsl:text>"</xsl:text>
        <xsl:value-of select="name()"/>" : "<xsl:value-of select="text()"/>
        <xsl:text>"</xsl:text>
    </xsl:if>
    <xsl:if test="count(child::*) = 0 and text() and @*">
        <xsl:text>: "</xsl:text>
        <xsl:value-of select="text()"/>
        <xsl:text>"</xsl:text>
    </xsl:if>
    <xsl:text>}</xsl:text>
    <xsl:if test="position() &lt; last()">, </xsl:if>
</xsl:template>
<xsl:template match="@*" mode="attr">
    <xsl:text>"</xsl:text>
    <xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>
    <xsl:text>"</xsl:text>
    <xsl:if test="position() &lt; last()">,</xsl:if>
</xsl:template>
<xsl:template match="node/@TEXT | text()" name="removeBreaks">
    <xsl:param name="pText" select="normalize-space(.)"/>
    <xsl:choose>
        <xsl:when test="not(contains($pText, '&#xa;'))">
            <xsl:copy-of select="$pText"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="concat(substring-before($pText, '&#xd;&#xa;'), ' ')"/>
            <xsl:call-template name="removeBreaks">
                <xsl:with-param name="pText" select="substring-after($pText, '&#xd;&#xa;')"/>
            </xsl:call-template>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

于 2015-05-28T14:25:00.307 に答える
0

ドキュメント要素のすべての子要素を変数に格納する場合は、次のようにします。

<xsl:variable name="myVar" select="/*/*"/>

ただし、スタイルシートで文字列を生成したい場合は、これが解決策になる可能性があります。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:output method="text"/>

  <xsl:template match="/*">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <xsl:template match="*">
    <!-- We write the opening tag -->
    <xsl:value-of select="concat('&lt;',local-name())"/>
    <!-- Now, all attributes -->
    <xsl:apply-templates select="@*"/>
    <!-- Depending on whether we have an empty element or not, 
         we're adding the content or closing it immediately -->
    <xsl:choose>
      <xsl:when test="node()">
        <!-- Close opening tag -->
        <xsl:value-of select="'&gt;'"/>
        <!-- Add the content -->
        <xsl:apply-templates select="node()"/>
        <!-- Write closing tag -->
        <xsl:value-of select="concat('&lt;/',local-name(),'&gt;')"/>
      </xsl:when>
      <xsl:otherwise>
        <!-- Create empty element by closing tag immediately -->
        <xsl:value-of select="'/&gt;'"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*">
    <!-- Write an attribute -->
    <xsl:value-of select="concat(' ',local-name(),'=&quot;',.,'&quot;')"/>
  </xsl:template>
</xsl:stylesheet>

テキストを生成します (したがって、整形式でない XML は得られません)。名前空間、コメント、処理命令、および属性内の引用符を処理しないため、少し単純化しすぎています。入力 XML にこれらのいずれかが含まれている場合は、スタイルシートを調整する必要があります。

于 2013-01-15T12:43:19.777 に答える