簡體   English   中英

如何將Xpath語法轉換為XSL / XSLT語法?

[英]How to convert Xpath syntax to XSL/XSLT syntax?

我了解XPath語法的工作原理,並且可以編寫Xpath命令以從XML文件提取某些信息。
我想將XPath命令轉換為XSLT腳本,以便其他人可以在XML文件上運行該腳本以獲取相同的輸出。

例如

我有一個XML文件,例如,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
  <library>
        <section id="109196796">
            <master_information>
                <shelf_identifier>
                    <identifier type="CodeX" type_id="2">LB1500605917</identifier>
                    <identifier type="Common Code" type_id="15">150060591</identifier>
                </shelf_identifier>
                <shelf_master>
                    <section_type>1</section_type>
                    <book_type>3</book_type>
                </shelf_master>
            </master_information>
        </section>
        <section id="109196798">
            <master_information>
                <shelf_identifier>
                    <identifier type="CodeX" type_id="2">LB0777775917</identifier>
                    <identifier type="Common Code" type_id="15">077777591</identifier>
                </shelf_identifier>
                <shelf_master>
                    <section_type>1</section_type>
                    <book_type>3</book_type>
                </shelf_master>
            </master_information>
        </section>
        <section id="109196800">
            <master_information>
                <shelf_identifier>
                    <identifier type="CodeX" type_id="2">LB2589165917</identifier>
                    <identifier type="Common Code" type_id="15">258916591</identifier>
                </shelf_identifier>
                <shelf_master>
                    <section_type>1</section_type>
                    <book_type>3</book_type>
                </shelf_master>
            </master_information>
        </section>
  </library>


如果我運行下面的XPath命令,

//identifier[@type='CodeX']

我得到的輸出:

LB1500605917
LB0777775917
LB2589165917

..這是預期的。 現在,我嘗試將XPath命令轉換為XSL語法,如下所示:

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:variable name="return">
    <xsl:text>
</xsl:text> <!-- defined a line break -->
    </xsl:variable>
    <xsl:template match="//library"></xsl:template>
    <xsl:template match="//section/master_information/shelf_identifier/identifier"> 
        <xsl:value-of select="@type='CodeX'"/>
        <xsl:value-of select="$return"/> <!-- this basically puts a line break -->
    </xsl:template>
</xsl:stylesheet>

XSL似乎是正確的。 但是,不會生成任何輸出。

我是XSL / XSLT的新手。 我究竟做錯了什么?

您可以做的是添加一個與xpath匹配相同節點的模板,然后輸出值和換行符...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text"/>
  <!--The strip-space isn't completely necessary. I just always include it in my
  default stylesheets. It strips whitespace. You can preserve whitespace with
  xsl:preserve-space. See https://www.w3.org/TR/xslt#strip for more details.-->
  <xsl:strip-space elements="*"/>

  <!--Suppress output of text nodes by built-in templates.-->
  <xsl:template match="text()"/>

  <!--Match "indentifier" elements that contain a "type" attribute
  with the value of "CodeX".-->
  <xsl:template match="identifier[@type='CodeX']">
    <!--Output the value of the current context ("identifier") concatenated with
    a newline. ("&#xA;" is a hex entity reference. You could also use a decimal
    reference ("&#10;")). You could use either of these references as the value
    of a variable too (or even declare it as an entity).
    I use normalize-space() instead of . to clean up any additional spaces.
    See https://www.w3.org/TR/xpath/#function-normalize-space for details.-->
    <xsl:value-of select="concat(normalize-space(),'&#xA;')"/>
  </xsl:template>

</xsl:stylesheet>

注意匹配text()的空模板。 通過XSLT的內置模板規則添加了此功能以抑制text()節點的輸出。

另請注意,我沒有在比賽中使用// 這也是由於內置規則; 它們默認允許遞歸處理。

為什么不簡單地做:

XSLT 1.0

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

<xsl:template match="/">
    <xsl:for-each select="//identifier[@type='CodeX']">
        <xsl:value-of select="."/>
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM