1

xmlファイルからデータを取得してテキストファイルに保存しています。以下はmy.xmlファイルです。

<?xml version="1.0"?>
<Parent>
<Action>POSTACTION</ActionState>
<Message><![CDATA[An Exception is thrown testFunctionAlpha()]]></Message>
<Property key="Direction"  value="IN"/>
<Property key="MethodName"  value="testFunctionAlpha"/>
<Property key="ReturnValue"  value="exception"/>
</Parent>

<Parent>
<Action>PREACTION</ActionState>
<Message><![CDATA[This is message of myFunction ]]></Message>
<Property key="Direction"  value="IN"/>
<Property key="MethodName"  value="myFunction"/>
<Property key="ReturnValue"  value="cmy::returnvalue"/>
</Parent>

xmlファイルにはそのようなレコードが複数あります。次のコマンドを使用して、このxmlを解析し、日付をテストファイルに保存しています。

xsltproc scan.xsl my.xml >> output.txt

以下は、xmlファイルのパーシングに使用されるscan.xslファイルの内容です。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text"/>
<xsl:template match="Parent">
<xsl:variable name="mesg" select="./Message"/>
<xsl:if test="$mesg = 'An Exception is thrown testFunctionAlpha()'">
  <xsl:value-of select="./Action"/><xsl:text> </xsl:text>
  <xsl:value-of select="$mesg"/><xsl:text> </xsl:text>
  <xsl:apply-templates select="Property[@key='Direction']"/><xsl:text> </xsl:text>
  <xsl:apply-templates select="Property[@key='MethodName']"/><xsl:text> </xsl:text>
  <xsl:apply-templates select="Property[@key='ReturnValue']"/>
</xsl:if>
</xsl:template>

  <xsl:template match="Property"><xsl:value-of select="@value"/></xsl:template>

</xsl:stylesheet>

日付をテキストファイルに保存したいのですが、タグ値が「例外がスローされますtestFunctionAlpha()」のタグのみです。上記のコードを使用してこれを取得できますが、Output.txtには次のものが含まれます。タグ。その空の行を回避する方法は?そのため、output.txtにはxsl形式に一致するデータのみが含まれます。

4

1 に答える 1

2

最初のテンプレートでルート要素をキャプチャし、その後はマップする要素にのみテンプレートを適用する場合は、組み込みのデフォルトの処理テンプレートが適用されないようにすることができます。

また、改行の別のソースは、いずれかのtext()空白が追加されている場合、normalize-space()関数を使用してこれを削除することができます。xsl:selects

サンプルの入力xmlが無効であるため、ラッパールート要素(xml)を追加し、ActionResult終了タグをに変更しましたAction

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="/xml">
        <xsl:apply-templates select="Parent"/>
    </xsl:template>

    <xsl:template match="Parent">
        <xsl:variable name="mesg" select="Message"/>
        <xsl:if test="$mesg = 'An Exception is thrown testFunctionAlpha()'">
            <xsl:value-of select="Action"/>
            <xsl:text> </xsl:text>
            <xsl:value-of select="$mesg"/>
            <xsl:text> </xsl:text>
            <xsl:apply-templates select="Property[@key='Direction']"/>
            <xsl:text> </xsl:text>
            <xsl:apply-templates select="Property[@key='MethodName']"/>
            <xsl:text> </xsl:text>
            <xsl:apply-templates select="Property[@key='ReturnValue']"/>
        </xsl:if>
    </xsl:template>

    <xsl:template match="Property">
        <xsl:value-of select="@value"/>
    </xsl:template>

</xsl:stylesheet>

入力XML:

<xml>
    <Parent>
        <Action>POSTACTION</Action>
        <Message><![CDATA[An Exception is thrown testFunctionAlpha()]]></Message>
        <Property key="Direction"  value="IN"/>
        <Property key="MethodName"  value="testFunctionAlpha"/>
        <Property key="ReturnValue"  value="exception"/>
    </Parent>

    <Parent>
        <Action>PREACTION</Action>
        <Message><![CDATA[This is message of myFunction ]]></Message>
        <Property key="Direction"  value="IN"/>
        <Property key="MethodName"  value="myFunction"/>
        <Property key="ReturnValue"  value="cmy::returnvalue"/>
    </Parent>
</xml>

結果:

POSTACTION例外がスローされますtestFunctionAlpha()INtestFunctionAlpha例外

編集

ルートをキャプチャしたくない場合は、text()組み込みのテンプレートだけを抑制してオーバーライドすることもできます。

<xsl:template match="text()"/>
于 2012-10-09T08:01:17.763 に答える