Как вставить атрибут во все дочерние узлы - PullRequest
3 голосов
/ 07 марта 2011

Пожалуйста, найдите под моей страницей xform.

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
    xmlns:exforms="http://www.exforms.org/exf/1-0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xhtml:head>
        <xforms:instance id="instanceData">
            <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <fruits>
                    <fruit>
                        <fruit-name>Mango</fruit-name>
                    </fruit>
                    <fruit>
                        <fruit-name>Apple</fruit-name>
                    </fruit>
                    <fruit>
                        <fruit-name>Banana</fruit-name>
                    </fruit>
                </fruits>
            </form>
        </xforms:instance>
    </xhtml:head>
</xhtml:html>

Я хотел бы вставить атрибут вкус = "хорошо" во все теги с фруктами, как показано ниже

<fruit-name taste="good">

Я пробовал следующие способы добиться того же, но он всегда вставляет атрибут только к первому названию фрукта.

<xforms:insert ev:event="xforms-model-construct-done" 
  context="instance('instanceData')/fruits/fruit/fruit-name" 
  origin="xxforms:attribute('taste','good')" />

<xforms:insert ev:event="xforms-model-construct-done" 
  context="instance('instanceData')/fruits/fruit[position() &gt; 0]/fruit-name" 
  origin="xxforms:attribute('taste','good')" />

Пожалуйста, предложите способ вставки этого атрибута во все узлы фруктового имени при выстреле. Поскольку список фруктов динамичен, нам нужно динамическое решение для него.

Ответы [ 2 ]

0 голосов
/ 08 марта 2011

Атрибут расширения xxforms:iterate - ваш друг здесь. Следующее сделает свое дело. И в этом случае это даже проще, чем XSLT;).

<xforms:insert ev:event="xforms-model-construct-done"
               xxforms:iterate="fruits/fruit/fruit-name"
               context="." origin="xxforms:attribute('taste','good')"/>
0 голосов
/ 08 марта 2011

Я не знаю XForms, но эта задача чрезвычайно проста с XSLT :

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

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

 <xsl:template match="fruit-name">
  <fruit-name taste="good">
   <xsl:apply-templates select="node()|@*"/>
  </fruit-name>
 </xsl:template>
</xsl:stylesheet>

когда это преобразование применяется к предоставленному документу XML:

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xforms="http://orbeon.org/oxf/xml/xforms"
  xmlns:exforms="http://www.exforms.org/exf/1-0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xhtml:head>
        <xforms:instance id="instanceData">
            <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <fruits>
                    <fruit>
                        <fruit-name>Mango</fruit-name>
                    </fruit>
                    <fruit>
                        <fruit-name>Apple</fruit-name>
                    </fruit>
                    <fruit>
                        <fruit-name>Banana</fruit-name>
                    </fruit>
                </fruits>
            </form>
        </xforms:instance>
    </xhtml:head>
</xhtml:html>

желаемый, правильный результат получается:

<xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:xforms="http://orbeon.org/oxf/xml/xforms"
 xmlns:exforms="http://www.exforms.org/exf/1-0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xhtml:head>
    <xforms:instance id="instanceData">
      <form xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <fruits>
          <fruit>
            <fruit-name taste="good">Mango</fruit-name>
          </fruit>
          <fruit>
            <fruit-name taste="good">Apple</fruit-name>
          </fruit>
          <fruit>
            <fruit-name taste="good">Banana</fruit-name>
          </fruit>
        </fruits>
      </form>
    </xforms:instance>
  </xhtml:head>
</xhtml:html>
...