5

XSLT (Postgres への一括読み込み用) を使用して、XML ファイルをフラットなパイプ区切りファイルに変換しようとしています。出力の最後の列をノードの実際の XML にしたいと思います (後処理とデバッグを追加するため)。例えば:

<Library>
  <Book id="123">
    <Title>Python Does Everythig</Title>
    <Author>Smith</Author>
  </Book>

  <Book id="456">
    <Title>Postgres is Neat</Title>
    <Author>Wesson</Author>
  </Book>
</Library>

生成する必要があります

Python Does Everything|Smith|<Book id="123"><Title>Python Does Everythig</Title>Author>Smith</Author></Book>
Postgres is Neat|Wesson|<Book id="456"><Title>Postgres is Neat</Title><Author>Wesson</Author></Book>

私の現在のXSLは

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:strip-space elements="*" />
  <xsl:output method="text" omit-xml-declaration="yes" indent="no" /> 
  <xsl:template match="//Book">
    <xsl:value-of select="Title" />
    <xsl:text>|</xsl:text>
    <xsl:value-of select="Author" />

    <!-- put in the newline -->
    <xsl:text>&#xa;</xsl:text>
  </xsl:template>
</xsl:stylesheet>    
4

2 に答える 2

9

これが推奨される解決策かどうかはわかりませんが、出力方法を xml に設定してからxsl:copy-of関数を使用してみてください。

したがって、次の XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
  <xsl:strip-space elements="*" /> 
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />  
  <xsl:template match="//Book"> 
    <xsl:value-of select="Title" /> 
    <xsl:text>|</xsl:text> 
    <xsl:value-of select="Author" /> 
    <xsl:text>|</xsl:text>  
    <xsl:copy-of select="." />
    <!-- put in the newline --> 
    <xsl:text>&#xa;</xsl:text> 
  </xsl:template> 
</xsl:stylesheet>  

サンプル XML に適用すると、次の出力が生成されます

Python Does Everythig|Smith|<Book id="123"><Title>Python Does Everythig</Title><Author>Smith</Author></Book>
Postgres is Neat|Wesson|<Book id="456"><Title>Postgres is Neat</Title><Author>Wesson</Author></Book>
于 2012-05-14T15:50:52.857 に答える
0

それを試してください:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
        <xsl:apply-templates select="//Book"/>
    </xsl:template>
    <xsl:template match="Book">
        <xsl:value-of select="Title" />
        <xsl:text>|</xsl:text>
        <xsl:value-of select="Author" />
        <xsl:text>|</xsl:text>
        <xsl:apply-templates select="." mode="outputTags"/>
    </xsl:template>
    <xsl:template match="*"  mode="outputTags">
        <xsl:text>&lt;</xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:apply-templates select="@*"/>
        <xsl:text>></xsl:text>
        <xsl:apply-templates mode="outputTags"/>
        <xsl:text>&lt;/</xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:text>></xsl:text>
        <xsl:if test="self::Book">
            <xsl:text>&#x0A;</xsl:text>
        </xsl:if>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:text> </xsl:text>
        <xsl:value-of select="local-name()"/>
        <xsl:text>="</xsl:text>
        <xsl:value-of select="."/>
        <xsl:text>"</xsl:text>
    </xsl:template>
</xsl:stylesheet>

入力ファイルから次の結果が生成されます。

Python Does Everythig|Smith|<Book id="123"><Title>Python Does Everythig</Title><Author>Smith</Author></Book>
Postgres is Neat|Wesson|<Book id="456"><Title>Postgres is Neat</Title><Author>Wesson</Author></Book>
于 2012-05-14T15:07:24.330 に答える