【问题标题】:Read XML file inside XSLT and using that to construct new XML在 XSLT 中读取 XML 文件并使用它来构造新的 XML
【发布时间】:2013-05-01 18:05:23
【问题描述】:

我想在 XSLT 中读取一个 XML 文件并检查它的节点。如果它们与我正在应用转换的 XML 节点的值匹配,则获取 XML 节点的值并使用它来构造新的 XML 结构。

这里有一个例子来说明我的问题。我想很多人可以参考这个问题来实现类似的功能。

Referenced.xml

<xml>
  <root>
    <Id id = "1">
      <fields>
        <field>
          <name> Name1 </name>
          <value> Val1 </value>
        </field>
        <field>
          <name> Name2 </name>
          <value> Val2 </value>
        </field>
      </fields> 
    </Id>
    <Id id = "2">
    ...
    </Id>
  </root>
</xml>

Xml.xml

<XML>
  <Fields>
   <Id id = "1">
    <F1> Value1 </F1>
    <F2> Value2 </F2>
    <F1> Value3 </F1>
    <F4> Value4 </F4>
 </Id>
  </Fields>
</XML>

现在,我想创建一个转换,它将遍历 XML 文件 (Referenced.xml) 并检查两个 xml 匹配项中的 Id 位置,然后在该 id 中,Name1 = F1 以及无论它在哪里,获取 ' value' 并创建一个 XML 结构,如

<outputXml>
  <Field id="Val1">
    <val> Value1 </val>
  </Field>
  <Field id="Val2">
    <val> Value2 </val>
  </Field> ... and so on
</outputXml>

我知道我必须使用document(),但我不确定你如何遍历xsl 中的Referenced.xml 并使用if,否则以实现所需的功能?

【问题讨论】:

  • 您的 XML 格式错误, 无效。请提供有效的 XML。
  • 已添加。忘记加引号了。再次检查
  • 对不起,它仍然是错误的。您需要指定一个属性名称,例如
  • 对不起,我再次编辑是有效的。
  • 有人刚刚编辑了我的更改。你如何在这里防止这种情况?虽然我又恢复了有效。

标签: xml xslt


【解决方案1】:

检查这个:

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

  <xsl:param name="file1" select="document('Referenced.xml')"/>
  <xsl:param name="file2" select="document('Xml.xml')"/>

  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>

  <xsl:template match="/">
    <outputXml>
      <xsl:for-each select="$file1//Id">
        <xsl:variable name="ReferencedID" select="@id"/>
        <xsl:choose>
          <xsl:when test="$ReferencedID = $file2//Id/@id">
            <xsl:for-each select="fields/field">
              <Field id="{normalize-space(value)}"><xsl:value-of select="value"/></Field>
            </xsl:for-each>
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </outputXml>
  </xsl:template>

</xsl:stylesheet>

获取输出:

<outputXml>
   <Field id=" Val1 "> Val1 </Field>
   <Field id=" Val2 "> Val2 </Field>
</outputXml>

【讨论】:

  • 您的代码有效,但唯一的问题是 Xml.xml 不是一个新文件,而是一个将应用 XSL 转换的文件。您能否修改您的代码,以便我接受您的回答。
  • 在输出中我也想要 Value1 和 Value2 而不是 Val1 和 Val2 :)
【解决方案2】:

这是一段工作代码:

<!-- loads a file like resources/strings-en.xml into a variable document-->
<xsl:variable 
    name="messages" 
    select="document(concat('resources/strings-', $lang, '.xml'))/my:strings"
/>

...

<!-- uses the loaded document for selecting -->
<xsl:template name="localized-string">
    <xsl:param name="name"/>
    <xsl:value-of select="$messages/my:string[@name=$name]"/>
</xsl:template>

如您所见,一旦您将文档读入变量,它看起来就像一个普通文档,您可以应用 XPath 表达式来遍历它,等等。

【讨论】:

    【解决方案3】:

    试试这个 您的 XML 文件 Chapters.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="ditaXSL.xsl"?>
    <map>
        <title>Syncro phone user guide</title>
        <topicref href="gettingStarted.dita"/>
    </map>
    

    您的 XSl 名称:ditaXSL.xsl

    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:f="Functions"
        version="2.0">
        <xsl:template match="map">
            <html>
                <title>
                    <xsl:value-of select="title" />
                </title>
                <h4>
                    <xsl:value-of select="title" />
                </h4>
    
                <body>
                    <table>
                        <tr>
                            <th>TOPIC REF</th>
                        </tr>
    
                        <xsl:for-each select="topicref">
                            <tr>
                                <td>
                                    <xsl:apply-templates select="document(@href)/topic" />
                                </td>
                            </tr>
                        </xsl:for-each>
                    </table>
                </body>
    
            </html>
        </xsl:template>
    
        <xsl:template match="topic">
            <xsl:apply-templates />
        </xsl:template>
    
    
        <xsl:template match="title">
            Chapter :
            <h6>
                <xsl:value-of select="." />
            </h6>
        </xsl:template>
    
        <xsl:template match="body">
            Description :
            <h5>
                <xsl:value-of select="." />
            </h5>
        </xsl:template>
    </xsl:stylesheet>
    

    xml 文件中提到的示例 gettingStarted.dita 文件。

    <?xml version="1.0" encoding="UTF-8"?>
    <topic>
      <title>Getting started</title>
      <body>This is introduction</body>
    </topic>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多