xslを複数回変更しましたが、正しい方法が見つかりませんでした。ここに私が欲しいものがここにあります:特定のタグがすでに存在する場合、複数のタグを同じレベルに追加します。
私は今、説明がないすべての不足しているページを見つけることを試みます。見つからない場合は、そのページの説明を追加します。存在する場合は説明を変更します。 pageX to pageXDescriptionのStringは常に同じです。ここで
は私の短い例のxmlです:
<BOOK>
<PAGE NAME='page1' VALUE='coolText'/>
<PAGE NAME='Description1' VALUE='coolDescription'/>
<PAGE NAME='page2' VALUE='moreText'/>
<PAGE NAME='page3' VALUE='aLotMoreText'/>
<PAGE NAME='Description3' VALUE='aLotMoreDescriptions'/>
</BOOK>
私はそのような何かでそれを試してみました:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- define output settings and header -->
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" media-type="string" encoding="ISO-8859-1" doctype-system="deftable.dtd"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page1']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page1'"/>
<xsl:with-param name="description" select="'Description1'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page2']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page2'"/>
<xsl:with-param name="description" select="'Description2'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template match="BOOK[PAGE/@NAME='page3']">
<xsl:copy>
<xsl:call-template name="create_missing_description_pages">
<xsl:with-param name="page" select="'page3'"/>
<xsl:with-param name="description" select="'Description3'"/>
<xsl:with-param name="new_description" select="'newContent'"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<!-- Function to generate missing XML Tags -->
<xsl:template name="create_missing_description_pages">
<xsl:param name="page"/>
<xsl:param name="description"/>
<xsl:param name="new_description"/>
<xsl:apply-templates select="@*|VARIABLE[@NAME=$page]/preceding-sibling::node()"/>
<xsl:apply-templates select="VARIABLE[@NAME=$page]"/>
<xsl:if test="not(VARIABLE/@NAME=$description)">
<xsl:element name="PAGE">
<xsl:attribute name="NAME"><xsl:value-of select="$description"/></xsl:attribute>
<xsl:attribute name="VALUE"><xsl:value-of select="$new_description"/></xsl:attribute>
</xsl:element>
</xsl:if>
<xsl:apply-templates select="VARIABLE[@NAME=$page]/following-sibling::node()"/>
</xsl:template>
<xsl:template match="BOOK/PAGE">
<xsl:copy>
<xsl:choose>
<xsl:when test="@NAME='Description1'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<xsl:when test="@NAME='Description2'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<xsl:when test="@NAME='page3Description'">
<xsl:attribute name="NAME"><xsl:value-of select="@NAME"/></xsl:attribute>
<xsl:attribute name="VALUE">newContent</xsl:attribute>
</xsl:when>
<!-- other child items will just be copied -->
<xsl:otherwise>
<xsl:copy-of select="@*"/>
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
すべての説明が不足している場合、私は、すべてのケースで期待していものです、すべての説明が利用可能な場合:
<BOOK>
<PAGE NAME='page1' VALUE='coolText'/>
<PAGE NAME='Description1' VALUE='newContent'/>
<PAGE NAME='page2' VALUE='moreText'/>
<PAGE NAME='Description2' VALUE='newContent'/>
<PAGE NAME='page3' VALUE='aLotMoreText'/>
<PAGE NAME='Description3' VALUE='newContent'/>
</BOOK>
ページ3がない場合は、ページも必要ありません。Descriptio n。
私はそう思います、それは理解できます。
私の論理的な失敗はどこであり、どのように修正するのかヒントをいただきありがとうございます。
ビョルン
は 'page1'と' page1Description'ていますので、あなたの本当の名前にXML入力、つまり、XSLTの名前が一致するというスキームはありますか? –
こんにちはマーティン、応答のおかげで。はい、彼らは本当の名前の最後にありますが、私の問題を示すためだけにこの例を生成しました。実際のXMLははるかに複雑です。これは良いXML構造ではありませんが、入力構造を変更することはできません。私はあなたの質問に答えられることを願っています。 –