2017-01-20 3 views
0

xsltのfor-eachを使用して要素内の属性値を取得しようとしています。Xslt - for eachが要素の中に目的の "属性"を生成しない

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <ns2:Example xmlns:ns2="http://example.com/internal"> 
     <inspection-result version="1"> 
      <form title="Form 1" history="true"> 
       <property name="id" type="string"/> 
       <property name="name" type="string"/> 
       <property name="default" type="string"/> 
       <property name="time" type="string"/> 
       <property name="status" type="string"/> 
       <action name="click"/> 
      </form> 
      <next>Form2</next> 
     </inspection-result> 
    </ns2:Example> 

XSLT:

はすべて <property name="id" type="string"/>

入力XMLの名前のみ= "ID" とタイプが= "string" を所望の出力を得るためにいくつかのいずれかの助けを得ていますすることができ、として

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:ns2="http://example.com/internal" 
exclude-result-prefixes="ns2"> 
<xsl:template match="inspection-result"> 
    <inspection-result> 
    <xsl:attribute name="version"><xsl:value-of select="//inspection-result/@version"/></xsl:attribute> 
    <form> 
    <xsl:attribute name="title"><xsl:value-of select="//form/@title" /></xsl:attribute> 
    <xsl:attribute name="history"><xsl:value-of select="//form/@history" /></xsl:attribute> 
    <xsl:for-each select="//form/property"> 
    <property> 
     <xsl:attribute name="name"><xsl:value-of select="//form/property/@name" /></xsl:attribute> 
     <xsl:attribute name="type"><xsl:value-of select="//form/property/@type" /></xsl:attribute> 
    </property> 
    </xsl:for-each> 

    <action> 
    <xsl:attribute name="name"><xsl:value-of select="//action/@name" /></xsl:attribute> 
    </action> 
    </form> 
    <next> 
    <xsl:value-of select="//next" /> 
    </next> 
    </inspection-result> 
</xsl:template> 
</xsl:stylesheet> 

予想される出力:

<inspection-result version="1"> 
     <form title="Form 1" history="true"> 
      <property name="id" type="string"/> 
      <property name="printerName" type="string"/> 
      <property name="default" type="string"/> 
      <property name="createdTimeStamp" type="string"/> 
      <property name="status" type="string"/> 
      <action name="click"/> 
     </form> 
     <next>Form2</next> 
    </inspection-result> 

答えて

0

あなたは、私がすでにあるので、 nはformの文脈では、あなたは例えば、その子要素に相対パスを使用する必要があります。

<xsl:value-of select="@name" /> 

あなたが今持っているもの:

<xsl:value-of select="//form/property/@name" /> 

を選択絶対パスですすべて@name文書全体の属性で、ルートから始まります。また、XSLT 1.0では、xsl:value-of命令は、選択したノードセットの最初のノードの値のみを返します。


他の場所でも//の表現を過剰に使用しています。私はあなたとあなたのテンプレートを書き直すことをお勧め:

<xsl:template match="inspection-result"> 
    <inspection-result> 
     <xsl:attribute name="version"> 
      <xsl:value-of select="@version"/> 
     </xsl:attribute> 
     <form> 
     <xsl:attribute name="title"> 
      <xsl:value-of select="form/@title" /> 
     </xsl:attribute> 
     <xsl:attribute name="history"> 
      <xsl:value-of select="form/@history" /> 
     </xsl:attribute> 
     <xsl:for-each select="form/property"> 
      <property> 
       <xsl:attribute name="name"> 
        <xsl:value-of select="@name" /> 
       </xsl:attribute> 
       <xsl:attribute name="type"> 
        <xsl:value-of select="@type" /> 
       </xsl:attribute> 
      </property> 
     </xsl:for-each> 
     <action> 
      <xsl:attribute name="name"> 
       <xsl:value-of select="@name" /> 
      </xsl:attribute> 
     </action> 
     </form> 
      <next> 
       <xsl:value-of select="next" /> 
      </next> 
    </inspection-result> 
</xsl:template> 
+0

おかげで一度にlot..couldn't応答は、私のサービスの流れは完全にXSLTを学ぼうとthis..still使用を終え取得するwokringた:) – Ranjan

関連する問題