0

私はこのようなデータを持っています -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <key>{Salary}</key>
    <value>1000</value>
</item>

出力を次のようにしたい -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <Salary>1000</Salary>
</item>

値だけでなく編集します。他のタグがあり、そのうちの1つだけが空でないことが保証されている場合、変換の何が問題になっていますか? ソースとして Sean の XSLT 1.0 変換を使用しています。

入力 -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <key>{Salary}</key>
    <value />
    <value2>1000</value2>
    <value3 />
</item>

望ましい出力 -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <Salary>1000</Salary>
</item>

私の現在の変換 -

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="key">
<xsl:element name="{substring-before(substring-after(.,'{'),'}')}"> 
    <xsl:choose>
        <xsl:when test="value != ''">
            <xsl:value-of select="following-sibling::value" />
        </xsl:when>
        <xsl:when test="value2 != ''">
            <xsl:value-of select="following-sibling::value2" />
        </xsl:when>
        <xsl:when test="value3 != ''">
            <xsl:value-of select="following-sibling::value3" />
        </xsl:when>
        </xsl:choose>
    </xsl:element> 
</xsl:template>
</xsl:stylesheet>
4

1 に答える 1

1

XSLT 1.0 ソリューション ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="value" />

<xsl:template match="key">
 <xsl:element name="{substring-before(substring-after(.,'{'),'}')}"> 
   <xsl:value-of select="following-sibling::value" /> 
 </xsl:element> 
</xsl:template>

</xsl:stylesheet>

アップデート

ここにも XSLT 2 ソリューションがあります。これはテストされていません。

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="attribute()|text()|comment()|processing-instruction()">
  <xsl:copy/>
</xsl:template>

<xsl:template match="value" />

<xsl:template match="key">
 <xsl:element name="{fn:replace(.,'^\{(.*)\}$','$1')}"> 
   <xsl:value-of select="following-sibling::value" /> 
 </xsl:element> 
</xsl:template>

</xsl:stylesheet>
于 2013-06-21T16:00:24.043 に答える