1

xml メッセージに適用する 2 つの xslt 変換があります。1 つ目は、すべての名前空間とプレフィックスを削除することです。コードは次のとおりです。

<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()[not(self::*)]">
  <xsl:copy/>
 </xsl:template>

 <xsl:template match="*">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>
</xsl:stylesheet>

2 つ目は、最初の出力から要素を選択することです。

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <onSale><xsl:value-of select="//entry/content/product_product/sale_ok" /></onSale>
        <onlineOnly><xsl:value-of select="//entry/content/product_product/online" /></onlineOnly>
        <name><xsl:value-of select="//entry/content/product_product/name" /></name>
        <isbn><xsl:value-of select="//entry/content/product_product/isbn" /></isbn>
        <price><xsl:value-of select="//entry/content/product_product/price" /></price>
        <active><xsl:value-of select="//entry/content/product_product/active" /></active>
        <format><xsl:value-of select="//entry/content/product_product/format" /></format>
        <collection><xsl:value-of select="//entry/content/product_product/collection" /></collection>
        <dateParution><xsl:value-of select="//entry/content/product_product/date_parution"/></dateParution>
        <ean13><xsl:value-of select="//entry/content/product_product/ean13"/></ean13>
    </xsl:template>
</xsl:stylesheet>

2 つの変換を別々に行わずに、1 つの xslt 変換で 2 つを適用する方法を教えてください。ありがとう

4

1 に答える 1

0

お使いの環境でサポートされている XSLT のバージョンがわかりません。XSLT 2.0 を使用できます

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

<xsl:variable name="step1">
 <xsl:apply-templates mode="step1"/>
</xsl:variable>

 <xsl:template match="@*|node()[not(self::*)]" mode="step1">
  <xsl:copy/>
 </xsl:template>

 <xsl:template match="*" mode="step1">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates select="node()|@*" mode="step1"/>
  </xsl:element>
 </xsl:template>

    <xsl:template match="/">
      <xsl:apply-templates select="$step1//entry"/>
    </xsl:template>

    <xsl:template match="entry">
        <onSale><xsl:value-of select="content/product_product/sale_ok" /></onSale>
        <onlineOnly><xsl:value-of select="content/product_product/online" /></onlineOnly>
        <name><xsl:value-of select="content/product_product/name" /></name>
        <isbn><xsl:value-of select="content/product_product/isbn" /></isbn>
        <price><xsl:value-of select="content/product_product/price" /></price>
        <active><xsl:value-of select="content/product_product/active" /></active>
        <format><xsl:value-of select="content/product_product/format" /></format>
        <collection><xsl:value-of select="content/product_product/collection" /></collection>
        <dateParution><xsl:value-of select="content/product_product/date_parution"/></dateParution>
        <ean13><xsl:value-of select="content/product_product/ean13"/></ean13>
    </xsl:template>

</xsl:stylesheet>

ただし、さらにリファクタリングして、次のようなテンプレートを記述します

<xsl:template match="sale_ok">
  <onSale>
    <xsl:value-of select="."/>
  </onSale>
</xsl:template>

XSLT 1.0 を使用する場合は、変数の内容を apply-templates で処理するための拡張関数が必要です。

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

<xsl:variable name="step1">
 <xsl:apply-templates mode="step1"/>
</xsl:variable>

 <xsl:template match="@*|node()[not(self::*)]" mode="step1">
  <xsl:copy/>
 </xsl:template>

 <xsl:template match="*" mode="step1">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates select="node()|@*" mode="step1"/>
  </xsl:element>
 </xsl:template>

    <xsl:template match="/">
      <xsl:apply-templates select="exsl:node-set($step1)//entry"/>
    </xsl:template>

    <xsl:template match="entry">
        <onSale><xsl:value-of select="content/product_product/sale_ok" /></onSale>
        <onlineOnly><xsl:value-of select="content/product_product/online" /></onlineOnly>
        <name><xsl:value-of select="content/product_product/name" /></name>
        <isbn><xsl:value-of select="content/product_product/isbn" /></isbn>
        <price><xsl:value-of select="content/product_product/price" /></price>
        <active><xsl:value-of select="content/product_product/active" /></active>
        <format><xsl:value-of select="content/product_product/format" /></format>
        <collection><xsl:value-of select="content/product_product/collection" /></collection>
        <dateParution><xsl:value-of select="content/product_product/date_parution"/></dateParution>
        <ean13><xsl:value-of select="content/product_product/ean13"/></ean13>
    </xsl:template>

</xsl:stylesheet>

他方で、名前空間を排除したり無視したりしようとする質問が最近多く寄せられているのはなぜだろうかと思います。本当に XSLT 2.0 でそれを行いたいのであれば、

<xsl:template match="*:sale_ok">
  <onSale>
     <xsl:value-of select="."/>
  <onSale>
</xsl:template>

などで十分です。

于 2013-06-17T17:46:59.663 に答える