0
私は入力XMLファイルの両方と同じXSLコードを使用したいという点で2セットのXMLファイルを持っています。見出しレベルの整列を調整する必要があります
私の最初のxmlファイルがある:私はこのために使用
<Body>
<h1>Child1</h1>
<p>Class1</p>
<h2>Child2</h2>
<p>Class2</p>
<h3>Child3</h3>
<p>Class3</p>
<h4>Child4</h4>
<p>Class4</p>
</Body>
XSL:
<xsl:template match="Body">
<xsl:for-each-group select="*" group-starting-with="h1">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
<xsl:choose>
<xsl:when test="self::h2">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h3">
<xsl:choose>
<xsl:when test="self::h3">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<xsl:for-each-group select="current-group() except ." group-starting-with="h4">
<xsl:choose>
<xsl:when test="self::h4">
<topic>
<xsl:attribute name="id">topic_<xsl:number count="h1 | h2 | h3 | h4"/></xsl:attribute>
<title>
<xsl:apply-templates select="node()"/>
</title>
<body><xsl:apply-templates select="current-group() except ."/></body>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:when>
<xsl:otherwise>
<body><xsl:apply-templates select="current-group()"/></body>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</topic>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
私は以下のような出力を得た:
<topic id="topic_1">
<title>Child1</title>
<body>
<p>Class1</p>
</body>
<topic id="topic_2">
<title>Child2</title>
<body>
<p>Class2</p>
</body>
<topic id="topic_3">
<title>Child3</title>
<body>
<p>Class3</p>
</body>
<topic id="topic_4">
<title>Child4</title>
<body>
<p>Class4</p>
</body>
</topic>
</topic>
</topic>
</topic>
今私の第二のXMLがあります( h2を持たない):
私はこの出力を取得する必要があるため<topic id="topic_1">
<title>Child1</title>
<body>
<p>Class1</p>
</body>
<topic id="topic_2">
<title/>
<body/>
<topic id="topic_3">
<title>Child3</title>
<body>
<p>Class3</p>
</body>
<topic id="topic_4">
<title>Child4</title>
<body>
<p>Class4</p>
</body>
</topic>
</topic>
</topic>
</topic>
:第二XML用
私の期待される出力は次のようになります。どのように上記のXSLコードを変更する必要があります。私はこれのための新しいコーディングを必要としません。私はXSLコードの上のexcistingを編集したいと思います。誰も私にこれを助けることができますか?この質問に対して事前
既存のコードへの、簡単で、直接修正はありませんたとえば 'が欠落していて、入力構造が異なる場合など、明らかに、失敗した要素を数えたりグループ化したりすることはできません存在する。最初のステップで不足要素が追加され、2番目のステップで既存のコードが適用される2段階アプローチを検討しましたか?最初のステップを慎重に指定し実装する必要があります。 –
ありがとう@MartinHonnen。 2番目の方法のために別のテンプレートを作成しなければなりません。 – User501
異なるモードで不足している要素を追加し、そのモードで入力を処理し、その結果を変数に格納してから、既存のコードで変数の内容を処理する、最初の変換ステップを実装することをお勧めします。 @MartinHonnen。 –