2017-04-20 7 views
0

レコードの最後に要素を追加する必要があります。追加された要素の兄弟はオプションであり、存在してもいなくてもかまいません。末尾に<Property>要素が追加されている限り。最初のシナリオでは、<Property>要素がすでにテストファイルに存在する場合はそのまま残すべきですが、<User>要素の別の要素を<Property>要素の下に追加する必要があります。第2のシナリオでは、要素が存在しない場合は、最後に<Property>を追加するか、<Data>のすべての要素の後に配置する必要があります。他の要素はオプションです。XSLTのレコードの最後に要素を追加する方法

私はこの追加部品を追加する必要があります。

<Property> 
    <User> 
     <Value listID="AAA">Sample testing</Value> 
    </User> 
</Property> 

第一のシナリオ例:<Property>はテストファイルに存在している

INPUT

<Record> 
    <Data> 
     <Date>2017-04-25</Date> 
     <Name>John Kledd</Name> 
     <Address> 
     <BuildingNumber>4603</BuildingNumber> 
     </Address> 
     <Property> 
     <User> 
      <Value listID="123">Example Only</Value> 
     </User> 
     </Property> 
    </Data> 
</Record> 

E XPECTED OUTPUT

<Record> 
    <Data> 
     <Date>2017-04-25</Date> 
     <Name>John Kledd</Name> 
     <Address> 
     <BuildingNumber>4603</BuildingNumber> 
     </Address> 
     <Property> 
     <User> 
      <Value listID="123">Example Only</Value> 
     </User> 
     <User> 
      <Value listID="AAA">Sample testing</Value> 
     </User> 
     </Property> 
    </Data> 
</Record> 

第2のシナリオ例:<Property>はテストファイル

INPUT

<Record 
    <Data> 
     <Date>2017-04-25</Date> 
     <Name>John Kledd</Name> 
     <Address> 
     <BuildingNumber>4603</BuildingNumber> 
     </Address> 
    </Data> 
</Record> 

予想出力

012に存在しませんここで
<Record> 
    <Data> 
     <Date>2017-04-25</Date> 
     <Name>John Kledd</Name> 
     <Address> 
     <BuildingNumber>4603</BuildingNumber> 
     </Address> 
     <Property> 
     <User> 
      <Value listID="AAA">Sample testing</Value> 
     </User> 
     </Property> 
    </Data> 
</Record> 

は、私は両方のシナリオに使用私のXSLTです:私のXSLTは、両方のシナリオのために働いている

XSLT

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
<!-- identity transform --> 
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 
<!-- This part deletes the Property element --> 
<xsl:template match="Data/Property"/> 
<xsl:template match="Data/Address"> 
    <xsl:copy-of select="."/> 
    <xsl:choose> 
     <xsl:when test="not(following-sibling::Property)"> 
      <xsl:element name="Property"> 
       <xsl:element name="User"> 
        <xsl:element name="Value"> 
         <xsl:attribute name="name">AAA</xsl:attribute> 
         <xsl:value-of select="'Sample Testing'"/> 
        </xsl:element> 
       </xsl:element> 
      </xsl:element> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:element name="Property"> 
       <xsl:element name="User"> 
        <xsl:element name="Value"> 
         <xsl:attribute name="name"><xsl:value-of select="../UserArea/Property/NameValue/@name"/></xsl:attribute> 
         <xsl:value-of select="../Property/User/Value"/> 
        </xsl:element> 
       </xsl:element> 
       <xsl:element name="User"> 
        <xsl:element name="Value"> 
         <xsl:attribute name="name">AAA</xsl:attribute> 
         <xsl:value-of select="'Sample Testing'"/> 
        </xsl:element> 
       </xsl:element> 
      </xsl:element> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

私は<Address>要素を削除した場合、しかし、それは動作しませんでした。私はXSLT v2.0を使用しています。長すぎる場合は謝罪してください。ご協力ありがとうございました。

よろしく、

答えて

0

は、あなたは、単に行うことができませんでした。

XSLT

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<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:variable name="default-user"> 
    <User> 
     <Value listID="AAA">Sample testing</Value> 
    </User> 
</xsl:variable> 

<xsl:template match="Data[not(Property)]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <Property> 
      <xsl:copy-of select="$default-user"/> 
     </Property> 
    </xsl:copy> 
</xsl:template> 

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

</xsl:stylesheet> 
+0

はmichael.hor257k @どうもありがとうございます。 – Charlotte

関連する問題