转换后,输出中不包含XSLT属性

时间:2016-02-24 15:54:22

标签: xml xslt

我正在尝试实现一个简单的xslt,在根节点下添加一个节点。

这是我的input.xml文件:

x <- 2.3
as.integer(x) + (x %% 1 * 3.333)
[1] 2.9999

我正在实现xsl以在所有listitem节点之前放置一个“dummy”div。

这是xsl文件:

<?xml version="1.0" encoding="UTF-8"?>
<root a="a">
    <itemizedlist role="type6" id="ecls_bio_becls_a3_a43205230.SL2392155.512">
        <listitem id="ecls_bio_becls_a3_a43205230.SL6440405.513"><para id="ecls_bio_becls_a3_a43205230.SL35914597.514">Glasform mit Deckel auf dem Rost, Höhe 1</para></listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL35935428.515"><para id="ecls_bio_becls_a3_a43205230.SL6441139.516">Mikrowelle 600 Watt</para></listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL37145770.517"><para id="ecls_bio_becls_a3_a43205230.SL37145771.518">Schalotten, Lauch: 4 Minuten</para></listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL37151364.519"><para id="ecls_bio_becls_a3_a43205230.SL37151365.520">Fleisch, Gemüse, Nudeln: 10-12 Minuten</para></listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL42243432.521"><para id="ecls_bio_becls_a3_a43205230.SL42243433.522">danach</para></listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL42243434.523"><para id="ecls_bio_becls_a3_a43205230.SL42243435.524">Mikrowelle 1000 Watt</para></listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL42243436.525"><para id="ecls_bio_becls_a3_a43205230.SL42243437.526">Bouillon: 5-6 Minuten</para></listitem>
    </itemizedlist>
</root>

输出是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"  encoding="UTF-8" omit-xml-declaration="yes" />

    <xsl:template match="@* | node()">

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

    <xsl:template match="itemizedlist[@role='type6']">
    <xsl:copy>
        <div class="recipe_placeholder"/>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

问题是在输出中我看不到itemizedlist的属性,我在xsl转换上添加了一个“div”元素。输出显示为

<root a="a">
    <itemizedlist><div class="recipe_placeholder"/>
        <listitem id="ecls_bio_becls_a3_a43205230.SL6440405.513"><para id="ecls_bio_becls_a3_a43205230.SL35914597.514">Glasform mit Deckel auf dem Rost, Höhe 1</para>
</listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL35935428.515"><para id="ecls_bio_becls_a3_a43205230.SL6441139.516">Mikrowelle 600 Watt</para>
</listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL37145770.517"><para id="ecls_bio_becls_a3_a43205230.SL37145771.518">Schalotten, Lauch: 4 Minuten</para>
</listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL37151364.519"><para id="ecls_bio_becls_a3_a43205230.SL37151365.520">Fleisch, Gemüse, Nudeln: 10-12 Minuten</para>
</listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL42243432.521"><para id="ecls_bio_becls_a3_a43205230.SL42243433.522">danach</para>
</listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL42243434.523"><para id="ecls_bio_becls_a3_a43205230.SL42243435.524">Mikrowelle 1000 Watt</para>
</listitem>
        <listitem id="ecls_bio_becls_a3_a43205230.SL42243436.525"><para id="ecls_bio_becls_a3_a43205230.SL42243437.526">Bouillon: 5-6 Minuten</para>
</listitem>
    </itemizedlist>
</root>

期望的是:

<itemizedlist>

我做错了什么?是否有其他参数来保护属性值?

非常感谢您的回答!

1 个答案:

答案 0 :(得分:1)

您必须先复制<div> 的属性才能开始下一个元素<xsl:template match="itemizedlist[@role='type6']"> <xsl:copy> <xsl:apply-templates select="@*" /> <div class="recipe_placeholder"/> <xsl:apply-templates select="node()" /> </xsl:copy> </xsl:template>

//your Eintraege
List<String> entries = ...; 

//compile the pattern only once
Pattern startPattern= Pattern.compile("^=== (.+) ===");

//use a StringBuilder for better performance
StringBuilder entry = new StringBuilder();  
boolean inEntry = false;

for (String line : dr.getAllLines()){   
  //you could also create the matcher outside and call matcher.reset(line) instead
  Matcher matcher = startPattern.matcher( line );

  if( matcher.find() ) {
    if( inEntry ) {
      //we're in an entry already so add the current and start a new one
      entries.add( entry.toString() );
      entry.clear();
    }

    //now we're definitely in an entry
    inEntry = true;
  }
  //no entry start but in an entry already 
  else if( inEntry ) {
    //apply whatever replacements you want and add the line to the current entry
    entry.append( line.replace("foo", "bar" ) );
  }
}

//if we're still in an entry here we need to add it as it didn't already happen in the loop
if( inEntry ) {
   entries.add( entry.toString() );
   entry.clear();
}