1

この質問は関連しています

XMLで指定されたhtmlタグを生成するXSLT

私はxmlドキュメントを持っていて、xslを使用してhtmlタグを生成しました

<xsl:element name="{Type}" >

私が抱えている問題は、たとえばxmlでいくつかのhtml属性を指定したいということです

<Page>
  <ID>Site</ID>
  <Object>
    <ID>PostCode</ID>
    <Type>div</Type>
    <Attributes>
       <Attribute name="class">TestStyle</Attribute>
       <Attribute name="id">TestDiv</Attribute>
    </Attributes>
    <Class>display-label</Class>
    <Value>PostCode</Value>
  </Object>
</Page>

xsl:element に xsl を使用して 2 つの属性を設定する方法を知っている人はいますか?

ありがとう

4

3 に答える 3

4

前の質問で投稿したスタイルシートから構築すると、要素宣言内で各Attributes/Attribute要素を繰り返し処理し、構築中の要素の属性構築できます。

Objectそのforループ内の要素ノードに「立っている」ので、次のAttributes/Attributeように要素を反復処理できます。

<xsl:for-each select="Attributes/Attribute">
  <xsl:attribute name="{@name}"><xsl:value-of select="current()"/></xsl:attribute>
</xsl:for-each>

スタイルシートに適用:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
      </head>
      <body>
        <xsl:for-each select="Page/Object">
            <xsl:element name="{Type}" >
                <xsl:for-each select="Attributes/Attribute">
                    <xsl:attribute name="{@name}"><xsl:value-of select="current()"/></xsl:attribute>
                </xsl:for-each>
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

これは、同じ出力を実現する別の方法ですが、apply-templates代わりにfor-each.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="html" indent="yes"/>
  <xsl:template match="/">
    <html>
      <head>
      </head>
      <body>
        <xsl:apply-templates select="Page/Object" />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="Object">
    <xsl:element name="{Type}" >
       <xsl:apply-templates select="Attributes/Attribute" />
       <xsl:apply-templates select="Value" />
    </xsl:element>
  </xsl:template>

  <xsl:template match="Attribute">
     <xsl:attribute name="{@name}"><xsl:value-of select="."/></xsl:attribute>
  </xsl:template>

</xsl:stylesheet>
于 2010-01-18T17:00:53.473 に答える
2

ソース サンプルの要素を修正する必要があります。Attributes要素が閉じていません。

xsl:for-eachまたはxsl:apply-templatesを使用して、次のような要素select="Attributes/Attribute"を呼び出すことができます。xsl:attribute

<xsl:attribute name="{@name}"><xsl:value-of select="text()"/></xsl:attribute>

注意する必要があるのは、要素xsl:attributeに子を追加するすべてのものよりも前に来なければならないということです。{Type}

于 2010-01-18T16:46:36.053 に答える
1
<xsl:element name="Attribute">
  <xsl:attribute name="class">TestStyle</xsl:attribute>
  <xsl:attribute name="id">TestDiv</xsl:attribute>
</xsl:element>
于 2010-01-18T16:26:56.687 に答える