3

HTMLを作成するために解析したい以下のXMLファイルがあります。私の問題は、私が望むようにそれを解析できないことです。

私がやりたいことは<items>、htmlとして出力することです。したがって、 a<paragraph>を a<div>に、<image>その <img>子ノードをそのプロパティ 'src' および 'alt' にします。

<itemlist>
    <item>
        <paragraph>pA</paragraph>
        <image>
            <url>http://www.com/image.jpg</url>
            <title>default image</title>
        </image>
        <paragraph>pB</paragraph>
        <paragraph>pC</paragraph>
        <link target='#'>linkA</link>
        <paragraph>pD</paragraph>
        <link target='#' >linkB</link>
        <image>
            <url>http://www.com/image2.jpg</url>
            <title>default image 2</title>
        </image>
        </item>
        <item>      
        <paragraph>pB</paragraph>
        <paragraph>pC</paragraph>
        <image>
            <url>http://www.com/image2.jpg</url>
            <title>default image 2</title>
        </image>
        <link target='#'>linkA</link>
        <paragraph>pD</paragraph>
        <link target='#'>linkB</link>
        </item>
    </itemlist>

foreach ループを実行し、 <item>match='paragraph' の後に match='image' が続くなどのテンプレートを適用して値を書き込むと、すべて<paragraph>が の前に書き込まれ<image>、正しい出力が得られません。

以下は、私が期待している出力です。誰でもそれを行う方法を知っていますか?

<div id="item">
    <div>pA</div>
    <img src='http://www.com/image.jpg' title='default image' />
    <div>pB</div>
    <div>pC</div>
    <a href='#'>linkA</a>
    <div>pD</div>
    <img src='http://www.com/image2.jpg' title='default image 2' />
</div>
<div id="item">
    <div>pB</div>
    <div>pC</div>
    <img src='http://www.com/image2.jpg' title='default image 2' />
    <a href='#'>linkA</a>
    <div>pD</div>
    <a href='#'>linkB</a>
</div>

-----編集---- 現在、私はこのようなものを持っています

    <xsl:for-each select="itemlist/item">
<xsl:apply-templates select="paragraph"/>
<xsl:apply-templates select="link"/>


  <xsl:template match="paragraph">
<xsl:value-of select="." />
  </xsl:template>

  <xsl:template match="link">
<xsl:value-of select="." />
  </xsl:template>


</xsl:for-each>
4

1 に答える 1

2

これはあなたが探しているものですか?:

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

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

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

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

</xsl:stylesheet>
于 2013-01-07T20:14:27.507 に答える