2016-10-28 6 views
-1

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。

私はそう思います、それは理解できます。

私の論理的な失敗はどこであり、どのように修正するのかヒントをいただきありがとうございます。

ビョルン

+0

は 'page1'と' page1Description'ていますので、あなたの本当の名前にXML入力、つまり、XSLTの名前が一致するというスキームはありますか? –

+0

こんにちはマーティン、応答のおかげで。はい、彼らは本当の名前の最後にありますが、私の問題を示すためだけにこの例を生成しました。実際のXMLははるかに複雑です。これは良いXML構造ではありませんが、入力構造を変更することはできません。私はあなたの質問に答えられることを願っています。 –

答えて

1

あなたはこの単純に作ることができませんでした敬具:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" 
exclude-result-prefixes="xs"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<xsl:strip-space elements="*"/> 

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

<xsl:template match="PAGE"> 
    <xsl:variable name="pagenum" select="xs:integer(substring-after(@NAME, 'page'))" /> 
    <xsl:copy-of select="."/> 
    <PAGE NAME='Description{$pagenum}'> 
     <xsl:attribute name="VALUE"> 
      <xsl:choose> 
       <xsl:when test="$pagenum=1">newContent1</xsl:when> 
       <xsl:when test="$pagenum=2">newContent2</xsl:when> 
       <xsl:when test="$pagenum=3">newContent3</xsl:when> 
      </xsl:choose> 
     </xsl:attribute> 
    </PAGE>  
</xsl:template> 

<xsl:template match="PAGE[starts-with(@NAME, 'Description')]"/> 

</xsl:stylesheet> 
+0

こんにちはマイケル、悪い例を申し訳ありません。識別子page1などは、説明または値では使用できません。これはコンテンツの一例に過ぎません。私は私の質問を編集します。よろしくお願い致します。Björn –

+0

こんにちはMichael、私はそれを以前のより近い例に変更しました。ページと説明の数は実際には同じですが、値の内容ではありません。どうもありがとう。よろしくお願いしますBjörn –

+2

@Björn説明値はどこから来ていますか?それらを計算することができますか、ページ名または番号で検索できる配列がありますか? –

関連する問題