add xml:id to item and target to ref
#!/usr/bin/perl | |
use 5.012; | |
use warnings; | |
binmode STDOUT, ':utf8'; | |
use XML::LibXML; | |
use XML::LibXSLT; | |
my $xslt = XML::LibXSLT->new; | |
my $parser = XML::LibXML->new; | |
my $xpc = XML::LibXML::XPathContext->new; | |
$xpc->registerNs( 't', 'http://www.tei-c.org/ns/1.0' ); | |
my $source = $parser->load_xml( location => shift ); | |
my $xsl; $xsl = do { local $/; <DATA> }; | |
my $style_doc = XML::LibXML->load_xml( string => $xsl ); | |
my $stylesheet = $xslt->parse_stylesheet($style_doc); | |
$stylesheet->register_function( 'urn:p', 'id', sub { | |
my ($s) = @_; | |
$s = "$s"; | |
$s =~ /(\d+)/; | |
return $1 ? "i-$1" : "i-$s-XXX"; | |
}); | |
$stylesheet->register_function( 'urn:p', 'ref', sub { | |
my ($s) = @_; | |
$s = "$s"; | |
$s =~ /(\d+)/; | |
return $1 ? "#i-$1" : "#i-$s-XXX"; | |
}); | |
my $results = $stylesheet->transform( $source ); | |
my $xml = $stylesheet->output_as_chars($results); | |
say $xml; | |
__END__ | |
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:t="http://www.tei-c.org/ns/1.0" | |
xmlns:p="urn:p" | |
exclude-result-prefixes="t p" | |
xpath-default-namespace="http://www.tei-c.org/ns/1.0" | |
> | |
<xsl:output method="xml" version="1.0" indent="no" encoding="UTF-8"/> | |
<xsl:template match="t:item"> | |
<xsl:element namespace="http://www.tei-c.org/ns/1.0" name="item"> | |
<xsl:attribute name="xml:id"> | |
<xsl:value-of select="p:id(*/t:label/text())"/> | |
</xsl:attribute> | |
<xsl:apply-templates select="@*|*|text()|processing-instruction()|comment()"/> | |
</xsl:element> | |
</xsl:template> | |
<xsl:template match="t:ref"> | |
<xsl:element namespace="http://www.tei-c.org/ns/1.0" name="ref"> | |
<xsl:attribute name="target"> | |
<xsl:value-of select="p:ref(text())"/> | |
</xsl:attribute> | |
<xsl:apply-templates select="@*|*|text()|processing-instruction()|comment()"/> | |
</xsl:element> | |
</xsl:template> | |
<xsl:template match="@*|*|text()|processing-instruction()|comment()" priority="-1"> | |
<xsl:copy> | |
<xsl:apply-templates select="@*|*|text()|processing-instruction()|comment()"/> | |
</xsl:copy> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment